每次运行我的代码时都会出现以下错误。
JSON.typeMismatch(JSON.java:111)
如何解析用数组包装的JSONObject
?
这是我的代码。
public class FeedActivity extends AppCompatActivity implements OnFeedListener {
ListView listView;
FeedAdapter adapter;
ArrayList<Post> posts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
listView = (ListView)findViewById(R.id.listView);
adapter = new FeedAdapter(getApplicationContext(), R.layout.layout_feed_item);
listView.setAdapter(adapter);
FeedTask task= new FeedTask(this);
task.execute("http://www.botswanayouth.com/wp-json/wp/v2/posts");
}
@Override
public void onFeed(JSONArray array) {
posts= new ArrayList<>();
int length = array.length();
for (int i = 0; i < length; ++i) {
JSONObject object = array.optJSONObject(i);
Post post = new Post(object.optString("title"),object.optString("excerpt"),object.optString("featured_media"));
posts.add(post);
}
adapter.addAll(posts);
}
public class FeedTask extends AsyncTask<String, Void, JSONArray> {
private OnFeedListener listener;
public FeedTask(OnFeedListener listener) {
this.listener= listener;
}
@Override
protected JSONArray doInBackground(String... params) {
String url = params[0];
OkHttpClient client= new OkHttpClient();
Request.Builder builder= new Request.Builder();
Request request= builder.url(url).build();
try {
Response response= client.newCall(request).execute();
String json= response.body().string();
try {
JSONObject object= new JSONObject(json);
JSONArray array = object.optJSONArray("posts");
return array;
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONArray array) {
super.onPostExecute(array);
if (null== array)
return;
if (null != listener)
listener.onFeed(array);
}
}
public class FeedAdapter extends ArrayAdapter<Post> {
private int resource;
public FeedAdapter(Context context, int resource) {
super(context, resource);
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null==convertView) {
LayoutInflater inflater=LayoutInflater.from(getContext());
convertView=inflater.inflate(resource, null);
}
Post post = getItem(position);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView desc = (TextView) convertView.findViewById(R.id.description);
title.setText(post.title);
desc.setText(post.description);
return convertView;
}
}
public class Post {
public String title;
public String description;
public String thumbnail; //URL
public Post(String title, String desc, String thumbnail) {
this.title=title;
this.description=desc;
this.thumbnail=thumbnail;
}
}
}
答案 0 :(得分:0)
如果这是你的json
array-[
0object-{ "id" : number5878, "date" : string2016-09-20T14:04:01, "guid" :
object-{ "rendered" : stringhttp:\/\/botswanayouth.com\/?p=5877 },
"type" : stringpost, "link" : stringhttp:\/\/botswanayouth.com\/5877\/blow-blow-rappers-beef-dramaboi-vs-emtee\/,
"title" : object+{ ... }, "content" : object+{ ... }, "excerpt" :
object-{ "rendered" : string<p> Sessions next month at Stanbic Bank.<\/p>}
]
然后在您的初始响应中,您获得的json数组不是json对象。
所以这个
String json= response.body().string();
try {
JSONObject object= new JSONObject(json);
....
}
应该是
String json= response.body().string();
try {
JSONArray object= new JSONArray(json);
....
}
但老实说有点难以辨别,因为你所提供的内容与你的代码不符。例如,您正在寻找一个&#34;帖子&#34;你的代码中的数组,但你的示例json中没有。