1

我在网上找到了一个优秀的 JSON 解析器,我想在我的项目中使用它。会有很多 JSON 请求,所以我希望能够重用代码。这是 JSON 解析器:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) { 
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            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, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            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 {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

在我的主要活动中,我需要一种方法来检索解析器返回的 JSONObject。但是,它需要在后台线程中完成。

我不知道如何从 Asynctask 返回对象。我在考虑也许将解析器类包装在 Asynctask 中,并在完成时让它返回,但这给出了同样的难题。

有人可以帮忙吗?

4

2 回答 2

5

您可以将已经编写的类放入方法返回的AsyncTask地方。在陆地上,从(在后台线程上调用的方法)返回的值被传递给在主线程上调用的值。您可以使用通知您操作已完成,并直接通过您定义的自定义回调接口传递对象,或者仅在操作完成时调用以取回已解析的 JSON。因此,例如,我们可以使用以下内容扩展您的类:doInBackground()JSONObjectAsyncTaskdoInBackground()onPostExecute()onPostExecute()ActivityActivityAsyncTask.get()

public class JSONParser extends AsyncTask<String, Void, JSONObject> {
    public interface MyCallbackInterface {
        public void onRequestCompleted(JSONObject result);
    }

    private MyCallbackInterface mCallback;

    public JSONParser(MyCallbackInterface callback) {
        mCallback = callback;
    }

    public JSONObject getJSONFromUrl(String url) { /* Existing Method */ }

    @Override
    protected JSONObject doInBackground(String... params) {
        String url = params[0];            
        return getJSONFromUrl(url);
    }

    @Override
    protected onPostExecute(JSONObject result) {
        //In here, call back to Activity or other listener that things are done
        mCallback.onRequestCompleted(result);
    }
}

并从这样的活动中使用它:

public class MyActivity extends Activity implements MyCallbackInterface {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...existing code...

        JSONParser parser = new JSONParser(this);
        parser.execute("http://my.remote.url");
    }

    @Override
    public void onRequestComplete(JSONObject result) {
        //Hooray, here's my JSONObject for the Activity to use!
    }
}

此外,作为旁注,您可以在解析方法中替换以下所有代码:

is = httpEntity.getContent();           

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "n");
     }
    is.close();
    json = sb.toString();
} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}

有了这个:

json = EntityUtils.toString(httpEntity);

希望有帮助!

于 2012-07-11T19:18:43.827 回答
1

查看AsyncTask文档,第 3 个泛型类型参数是 Result,即后台计算结果的类型。

private class MyTask extends AsyncTask<Void, Void, JSONObject> {
    protected JSONObject doInBackground(Void... params) {
        ...
        return json;
    }
    protected void onPostExecute(JSONObject result) {
        // invoked on the UI thread
    }
}

使您的任务成为活动的内部类,创建活动的成员private JSONObject mJSON = null;onPostExecute()进行分配mJSON = result;

于 2012-07-11T19:09:54.657 回答