我正在開發Android應用程序,我需要從WordPress博客(具有特定標籤)的帖子。 JSON API
插件已安裝:https://wordpress.org/plugins/json-api/Android排空:期望BEGIN_ARRAY,但是BEGIN_OBJECT
在我的應用程序中,我使用Volley庫。我收到此錯誤:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
我的代碼:
String url = " http://christianconcepts.com/api/get_tag_posts/?tag_slug=appcontent ";
ListView postList;
List<Object> list;
Gson gson;
Map<String,Object> mapPost;
Map<String,Object> mapTitle;
String postTitle[];
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
gson = new Gson();
list = (List) gson.fromJson(s, List.class); // error line
postTitle = new String[list.size()];
for(int i=0;i<list.size();++i){
mapPost = (Map<String,Object>)list.get(i);
mapTitle = (Map<String, Object>) mapPost.get("title");
postTitle[i] = (String) mapTitle.get("rendered");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getActivity().getApplicationContext(), "Some error occurred", Toast.LENGTH_LONG).show();
}
});
RequestQueue rQueue = Volley.newRequestQueue(myView.getContext());
rQueue.add(request);
有人能幫助我,告訴我需要什麼,以使其發揮作用的變化?
本官方指南發表您的JSON respnse – rafsanahmad007
你顯然有一個JSON'object'但試圖解析它作爲json'數組'。 – injecteer
你必須創建一個模型類。 –