我有一个获取JSON数组的AsyncTask。我将如何返回JSON数组:
JSONArray channels = new Json().execute(foo, bar);package com.example.tvrplayer;
Eclips告诉我,我不能那样做,应该是:
AsyncTask<Object, Integer, JSONArray> channels = new Json().execute("http://192.168.2.136:8080/rest/channel/"+ linkid +"/"+ username, "GET");
Json异步类:
public class Json extends AsyncTask<Object, Integer, JSONArray> {
Json(){
super();
}
@Override
protected JSONArray doInBackground(Object... params) {
// Log.i("JSON",url);
String url = (String) params[0];
String method = (String) params[1];
InputStream is = null;
String result = "";
JSONArray jsonObject = null;
// HTTP
try {
HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
if ( method == "GET") {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} else if (method == "POST") {
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
} catch(Exception e) {
Log.e("JSON - 1 -", e.toString());
return null;
}
// Read response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
// result = result.substring(1,result.length()-1);
// Log.d("JSON result",result);
} catch(Exception e) {
Log.e("JSON - 2 -", e.toString());
return null;
}
// Convert string to object
try {
jsonObject = new JSONArray(result);
} catch(JSONException e) {
Log.e("JSON - 3 -", e.toString());
return null;
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONArray result)
{
super.onPostExecute(result);
final Message msg = new Message();
msg.obj = result;
}
}
这就是我想要完成的事情:
JSONArray channels = new Json().execute("http://192.168.2.136:8080/rest/channel/"+ linkid +"/"+ username, "GET");
try {
for (int i=0; i < channels.length(); i++) {
JSONObject channel_data = channels.getJSONObject(i);
String channelID = channel_data.getString("ChannelID").toLowerCase();
JSONArray json = new Json().execute("http://192.168.2.136:8080/rest/program/"+ linkid +"/"+ username +"/" + channelID, "GET");
答案 0 :(得分:3)
你不是来自return
的{{1}}。你指示AsyncTask
在每天打电话之前做一些事情,但它不会给你AsyncTask
任何事情。这就是为什么它被称为异步&#34;:你不等待它,它不会等你。
例如,将此代码与return
:
SyncTask
这意味着result = SyncTask();
label.setText(result);
行不会被执行,直到setText()
完成并产生SyncTask()
。它是同步的。相反,使用异步,您可以:
result
这带来了全新的world of trouble。我建议你看看new AsyncTask() {
@Override
void onPostExecute(result) {
label.setText(result)
}
}.start()
,它的工作方式相似但提供了更强大的抽象。
此外,我告诉你这一事实意味着你不了解很多事情。您可能想要了解相关文档,教程或文章。
答案 1 :(得分:1)
您不必从ASyncTask返回任何内容
@Override
protected void onPostExecute(JSONArray result)
{
super.onPostExecute(result);
channels = result
//<here you can use channels to integrate with other code>
}
这里的频道将被声明为类变量
JSONArray channels;
答案 2 :(得分:0)
execute(Runnable runnable)返回void。
在onPostExecute()中分配结果,如下所示:
渠道=结果; doSomething的(信道)