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
    }
}
Mike
  • 826
  • 11
  • 31

2 Answers2

0

you need to so something like this (careful, not tested)

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);
    }
}
SimonSays
  • 10,867
  • 7
  • 44
  • 59
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.

So you should use AsyncTask to reach your goal. So just create subclass of AsyncTask and take your JSONParser logic into doInBackground() method:

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.

   }

   ...

}
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106