0

I'm using the method below to extract to convert XML into JSON.!

  1. First I a pass the url for the api call of the xml and then extract the data and put it in String.
  2. Then using this Library, I convert the xml into JSON Object.
  3. Finally I use the json object data to get web data and populate my recyclerviews.

It works well, but sometimes it gives me ANR dialog..! any help?

  public static void getResponse(final String xmlLink, final Activity activity,
      final GetJSONRespone getjson) {

    Handler uiHandler = new Handler(Looper.getMainLooper());
    uiHandler.post(new Runnable() {
      @Override
      public void run() {
        URL url = null;
        BufferedReader in = null;
        try {
          url = new URL(xmlLink);

          in = new BufferedReader(
              new InputStreamReader(
                  url.openStream(), "UTF-8")); 

          String inputLine;
          System.setProperty("http.keepAlive", "false");  

          StringBuilder builder = new StringBuilder();
          while ((inputLine = in.readLine()) != null) {
            builder.append(inputLine);
          }
          String urlContent = builder.toString();

          XmlToJson xmlToJson = new XmlToJson.Builder(urlContent).build();

          JSONObject response = xmlToJson.toJson();
          Log.i("response: ", response.toString());

          getjson.getJSON(response);


        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          if (in != null) {
            try {
              in.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    });
  }
Alaa AbuZarifa
  • 1,171
  • 20
  • 39
  • 1
    Use asyntask for download data from API or use third party library like volly and Retrofit. – D.J Sep 18 '17 at 06:37
  • https://developer.android.com/training/articles/perf-anr.html#anr – Kuffs Sep 18 '17 at 06:40
  • Try using Volley or RetroFit for JSON response, and to handle ANR please go through this -https://developer.android.com/training/articles/perf-anr.html – Goblin シ Sep 18 '17 at 07:51

1 Answers1

1

This dialog appears when main thread of the application blocked for too long. So try to use ASYNCTASK to avoid this halt.

Zeeshan Arif
  • 509
  • 5
  • 15