来自Asynctask的JSON和网络操作?

Ste*_*oen 1 android json asynchronous

我在网上找到了一个优秀的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;

    }
}
Run Code Online (Sandbox Code Playgroud)

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

我无法弄清楚如何从Asynctask返回一个对象.我正在考虑将解析器类包装在Asynctask中,并在完成时将其返回,但这给出了同样的难题.

有人可以帮忙吗?

Dev*_*red 5

你可以让你已经写了类到AsyncTask其中的doInBackground()方法返回一个JSONObject.在AsyncTask陆地中,从doInBackground()(在后台线程上调用的方法)返回的值被传递到onPostExecute()主线程上调用的值.您可以onPostExecute()用来通知您Activity操作已完成,并通过您定义的自定义回调接口直接传递对象,或者在操作完成时仅通过Activity调用AsyncTask.get()来获取已解析的JSON.因此,例如,我们可以使用以下内容扩展您的类:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并从这样的Activity中使用它:

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!
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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());
}
Run Code Online (Sandbox Code Playgroud)

有了这个:

json = EntityUtils.toString(httpEntity);
Run Code Online (Sandbox Code Playgroud)

希望有帮助!