2017-09-18 1 views
0

XML을 JSON으로 변환하기 위해 추출하는 방법을 사용하고 있습니다.응용 프로그램이 응답하지 않는 것을 방지하는 방법 대화 상자가 표시되지 않습니다.

  1. 먼저 XML의 API 호출에 대한 URL을 전달한 다음 데이터를 추출하여 String에 넣습니다.
  2. Library을 사용하면 I 이 xml을 JSON 개체로 변환합니다.
  3. 마지막으로 json 객체 데이터 을 사용하여 웹 데이터를 가져 와서 내 recyclerviews를 채 웁니다.
그것은 잘 작동하지만, 때때로 나에게 ANR 대화 상자를 제공

..! 어떤 도움?

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(); 
      } 
      } 
     } 
     } 
    }); 
    } 
+1

API에서 데이터를 다운로드하거나 volly 및 Retrofit과 같은 제 3 자 라이브러리를 사용하려면 asyntask를 사용하십시오. –

+0

https://developer.android.com/training/articles/perf-anr.html#anr – Kuffs

+0

JSON 응답을 위해 Volley 또는 RetroFit을 사용해보고 ANR을 처리하려면 -https : //developer.android.com을 실행하십시오. /training/articles/perf-anr.html –

답변

1

이 대화 상자는 응용 프로그램의 주 스레드가 너무 오래 차단되었을 때 나타납니다. 따라서 ASYNCTASK를 사용하여이 중지를 피하십시오.

+0

당신은 나에게 그것을 보여줄 수 있습니까, 나는 그것을 전에 사용하지 않았습니까? –

+0

@SamZar https://xelsoft.wordpress.com/2014/11/28/asynctask-implementation-using-callback-interface/ 링크를 확인하십시오. –

관련 문제