2

我有一个获取 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");
4

3 回答 3

3

你不是returnAsyncTask. 您在收工之前指示AsyncTask要做的事情,但它不会return对您有任何帮助。这就是为什么它被称为“异步”:你不等待它,它不等你。

例如,将此代码与SyncTask

result = SyncTask();
label.setText(result);

这意味着在完成之前不会执行setText()该行并产生一个. 是同步的。相反,使用异步,您可以:SyncTask()result

new AsyncTask() {
    @Override
    void onPostExecute(result) {
        label.setText(result)
    }
}.start()

这带来了一个全新的麻烦世界。我建议您看一下Loaders,它们的工作方式类似,但提供了更强大的抽象。

另外,我告诉你这个事实意味着有很多事情你不明白。您可能想在 Google 上搜索相关文档、教程或文章。

于 2013-02-21T15:59:16.957 回答
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;
于 2013-02-21T16:01:36.380 回答
0

Asynctask 中的 execute(Runnable runnable) 返回 void。

在 onPostExecute() 中分配您的结果,如下所示:

渠道=结果;做某事(频道)

于 2013-02-21T15:59:59.793 回答