我正在使用Kitsu API制作一个应用程序。此时此刻,我有一个片段,允许我使用api搜索不同的动画,以获取名称、概要等信息。解析完信息后,我继续返回我的主要活动,将其传递给我的新片段,但当我初始化方法以返回我的主要活动时,它就崩溃了。我的问题是如何使用片段使用ASYNCTASK读取带有背景类的JSON并将其显示在新片段中?
我注意到它从我的片段跳了很多,在一个不同于我的主线的活动中,到一个在后台运行的班级,到我的主线,再到我的片段。以下是我的代码的图像。谢谢。
搜索片段代码,主要活动搜索相关方法,搜索结果后台任务,搜索结果片段。为了澄清,它在我到达我标记为crashpoint的字符串之前就停止了,并且没有到达搜索结果片段。我觉得把那段代码放在里面会很方便。这是我运行代码时遇到的错误。
读取 JSON 后,无需创建新的片段。只需发布一个读取带有异步任务的JSON的简单代码:
package com.example.compsci_734t;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class People extends Activity{
ArrayList<String> items = new ArrayList<String>();
static InputStream is = null;
//private static String url = "";
//private static String url = "http:...";
private static String url = "http....";
//URL requestUrl = new URL(url);
JSONArray people = null;
private static final String TAG_COURSES = "Courses";
static JSONObject jObj = null;
static String json = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.people);
new MyTasks().execute();
}
private class MyTasks extends AsyncTask<URL, Void, JSONObject> {
@Override
protected JSONObject doInBackground(URL... urls) {
// return loadJSON(url);
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
//HttpPost httpPost = new HttpPost(url);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
/*BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);*/
InputStream inputStream = is;
GZIPInputStream input = new GZIPInputStream(inputStream);
InputStreamReader reader = new InputStreamReader(input);
BufferedReader in = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
//System.out.println(line);
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
JSONArray people = new JSONArray(json);
//JSONArray people = new JSONArray(json);
for (int i = 0; i < people.length(); i++) {
//System.out.println(courses.getJSONObject(i).toString());
JSONObject p = people.getJSONObject(i);
// Storing each json item in variable
String person_id = p.getString("someString1");
items.add(person_id);
/*Log.v("--", "People: \n" + "\n UPI: " + person_id);*/
}
//jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
protected void onPostExecute(JSONObject json) {
ListView myListView = (ListView)findViewById(R.id.peopleList);
myListView.setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, items));
}
}
示例代码取自此答案。