【发布时间】:2019-04-23 11:07:23
【问题描述】:
我正在开发一个简单的新闻应用程序。 我需要从远程服务器以 JSON 格式获取数据,然后将其放在视图中。我使用 TabLayout 和 recyclerView 来显示数据类别和 Volley 用于查询这里没有 API。
TabLayout 是根据来自 JSON 的数据自动设置的,我在其中提取选项卡标题,每个选项卡的内容都显示在 recyclerView 上(文章标题、图像、内容、链接...)并在片段中呈现
我花了几个小时尝试调试它,但没有成功。但无论我做什么,都没有显示任何数据。不知道我做错了什么。
我知道这不是要求此类事情的正确地方,但我有点绝望,需要一些有经验的开发人员来解决我的问题。
它是如何工作的:
Activity 启动 BaseArticleFragment,它调用一个加载内容类别并将数据绑定到视图的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.fm = getSupportFragmentManager();
this.baseArticleFragment = new BaseArticleFragment();
FragmentTransaction ft = this.fm.beginTransaction();
ft.add(R.id.fragment_container, this.baseArticleFragment, TAB_LAYOUT_FRAGMENT_TAG);
ft.commit();
}
启动时,baseArticleFragment 在其 onActivityCreated() 方法中调用 loadCategories() 方法:
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
loadCategories();
}
这里是 loadCategories() 方法:
private void loadCategories(){
String url = "http://somesite.com/categories"; //link to grab the json data
ApplicationController.getInstance().addToRequestQueue(
new JsonObjectRequest(0, url, null, new Listener<JSONObject>() { //0 is the Volley code for GET method
@Override
public void onResponse(JSONObject jsonObject) {
BaseArticleFragment.categories = JSONParser.parseCategories(jsonObject);
BaseArticleFragment.this.mViewPager.setAdapter(
new RecyclerViewFragmentPagerAdapter(BaseArticleFragment.this.getChildFragmentManager(),
BaseArticleFragment.categories));
BaseArticleFragment.this.mTabLayout.setupWithViewPager(BaseArticleFragment.this.mViewPager);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError vError){
Log.d("BaseArticleFragment", "---Volley Error---");
Snackbar.make(BaseArticleFragment.this.mTabLayout, R.string.error_load_categories, Snackbar.LENGTH_SHORT)
.setAction(R.string.action_retry, new View.OnClickListener() {
@Override
public void onClick(View v) {
BaseArticleFragment.this.loadCategories();
}
}).show();
}
}));
}
我猜问题可能出在查询上,但不确定因为我认为我的逻辑很好
编辑:
这是我需要获取的 JSON 数据:
[
{
"name": "Topic 1",
"tid": "2",
},
{
"name": "Topic 2",
"tid": "3",
},
{
"name": "Topic 3",
"tid": "4",
},
{
"name": "Topic 4",
"tid": "5",
},
{
"name": "Topic 5",
"tid": "6",
},
{
"name": "Topic 6",
"tid": "1415",
},
{
"name": "Topic 7",
"tid": "1414",
},
{
"name": "Topic 8",
"tid": "1298",
},
{
"name": "Topic 9",
"tid": "1301",
},
{
"name": "Topic 10",
"tid": "1299",
},
{
"name": "Topic 11",
"tid": "1302",
},
{
"name": "Topic 12",
"tid": "1300",
},
{
"name": "Topic 13",
"tid": "1297",
}
]
编辑 2: 我忘记在我的 JSONPArser 类中粘贴 parseCategories() 的代码
public static ArrayList<Category> parseCategories(JSONObject jsonObject) {
ArrayList<Category> categoryArrayList = new ArrayList<>();
try {
JSONArray categories = jsonObject.getJSONArray("categories");
Category all = new Category();
all.setTid("0");
all.setName(ApplicationController.getInstance().getString(R.string.tab_all));
categoryArrayList.add(all);
for (int i = 0; i < categories.length(); i++) {
JSONObject catObject = categories.getJSONObject(i);
Category category = new Category();
category.setTid(catObject.getString("tid"));
category.setName(catObject.getString("name"));
categoryArrayList.add(category);
}
return categoryArrayList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
【问题讨论】:
-
您是否检查过您收到的是成功响应而不是错误?
-
日志上没有错误码,但是onErrorResponse(VolleyError vError)上的snackbar被触发了
-
在这种情况下,检查“volleyError”以查看返回的错误代码
-
没有发现错误代码
-
这很奇怪。如果它正在输入错误响应代码,那么 vError 中必须有一些东西说明它是什么。尝试查看此处:stackoverflow.com/questions/24700582/handle-volley-error 和此处:stackoverflow.com/questions/21867929/…,了解如何准确识别错误类型的示例。
标签: android json android-fragments android-tablayout