异步运行单个JSON解析器类

时间:2013-02-15 19:08:37

标签: android json asynchronous

目前,我有一个名为JSONParser的类 我想异步运行这个类中的函数,但我无法弄清楚如何让我暂时停止禁用strictmode。

我的课程如下......

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
    }
}

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

  

目前,我有一个名为JSONParser的类,我想在其中运行函数   这个类异步,但我无法弄清楚如何让我卡住   暂时禁用strictmode。

因此,您应该使用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.

   }

   ...

}