2012-11-17 4 views
0

저는 안드로이드 플랫폼에 익숙하지 않습니다. 전에 많은 json 페이지를 파싱했지만, 하나의 링크가 문제를 일으키고 있습니다. 다음 함수를 사용하여 json 객체를 반환합니다. 프로그램은 reader.readline()에서 멈추고 로딩은 멈추지 않습니다.android json 파싱이 로딩을 계속합니다.

public JSONObject getJSONFromUrl(String url) { 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 

    try { 

     HttpResponse httpResponse = httpClient.execute(new HttpGet(url)); 
     if (httpResponse.getStatusLine().getStatusCode()==200) 
     { 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     System.out.println("Unsupported Encoding"); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     System.out.println("Client Protocol"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.out.println("IO"); 
    } 

    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); 

     } 
     is.close(); 
     jsonStr = sb.toString(); 
    } catch (Exception e) { 
     System.out.println("Error here"); 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 

     jObj = new JSONObject(jsonStr); 

    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jObj; 

} 

아무 것도 제안 할 수 있습니까?

답변

1

나는 이것이 귀하의 경우에 응답을 끝내지 않은 서버 때문에 발생했음을 확신합니다. 먼저 "컬"을 클라이언트로 사용하여이를 분리하십시오.

EntityUtils.toString(entity); 

또한 두 번째 매개 변수로 인코딩을 지정할 수 있습니다

또한이 같은 EntitUtils를 사용하는 경우 문자열에 몸을 인출 할 수있는 쉬운 방법이있다. http://developer.android.com/reference/org/apache/http/util/EntityUtils.html

+0

나는 엔티티도 시도했다. URL은 브라우저에서 정상적으로 작동하지만 거기에 문제가있는 것 같아요. 올바르게 작동하지만 문자열에서 json을 저장할 때 발생합니다.하지만 링크에서 가져 오지는 마십시오. –

관련 문제