0

Currently, I have a class called JSONParser I want to run functions in this class asynchronously but I can't figure out how to so I stuck with disabling strictmode at the moment.

My class is like as follows...

public class JSONParser {

    // Constructor
    public JSONParser() {

    }

    public JSONArray getJSONFromUrl(String url) {
        // HTTP Request
        try {
            // defaultHttpClient
            HttpClient httpClient = new DefaultHttpClient();
            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();
        }   

        // Streaming data into variable
        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 to parse the string to a JSON object
        try {
            jArr = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jArr;
    }

    public void parseJSON(JSONArray jsonArray) {
        // Parsing goes here
    }
}
4

2 回答 2

0

你需要这样的东西(小心,未经测试)

public class JSONParser {
    private OnParsedListener mListener;

    // Constructor
    public JSONParser(OnParsedListener l) {
        mListener = l;
    }

    public void getJSONFromUrlAsync(final String url) {
        new AsyncTask<Void, Void, String>() {
            private JSONArray arr;
            @Override
            protected String doInBackground(final Void... params) {
                arr = getJSONFromUrl(url);
            }
            @Override
            protected void onPostExecute(final String result) {
                mListener.onArrayResponse(arr)
            }
        }.execute();
    }



    public JSONArray getJSONFromUrl(String url) {
        // HTTP Request
        try {
            // defaultHttpClient
            HttpClient httpClient = new DefaultHttpClient();
            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();
        }   

        // Streaming data into variable
        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 to parse the string to a JSON object
        try {
            jArr = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        return jArr;
    }

    public void parseJSON(JSONArray jsonArray) {
        // Parsing goes here
    }
    public interface OnParsedListener{
        void onArrayResponse(JSONArray arr);
    }
}
于 2013-02-15T19:22:16.470 回答
0

目前,我有一个名为 JSONParser 的类,我想在这个类中异步运行函数,但我不知道如何去做,所以我现在坚持禁用严格模式。

所以你应该用它AsyncTask来达到你的目标。因此,只需创建子类AsyncTask并将您的JSONParser逻辑带入doInBackground()方法:

public class JSONWorker extends AsyncTask<String, Void, Void> {

   protected Void doInBackground(String... params) {
      JSONParser parser = new JSONParser();
      JSONArray arr = parser.getJSONFromUrl(params[0]);
      // do next work.

   }

   ...

}
于 2013-02-15T19:23:46.780 回答