2016-09-14 7 views
0

안드로이드에서 JSON 텍스트를 미리 인쇄하고 싶습니다. 내 문제는 내가 문자 0org.json.JSONException : 문자 0에서 끝나는 입력

에서 오류를 입력

끝을 얻을 나는이에 스레드를 많이 찾았지만 아무것도 내 문제에 대해 작동하는 것 같군 없다는 것입니다. 여기에 코드입니다 :

URL url; 
try { 
    url = new URL(https_url); 
    HttpsURLConnection con = (HttpsURLConnection)url.openConnection(); 
    FileOutputStream fileOutputStream1 = getApplicationContext().openFileOutput("Log.txt", MODE_PRIVATE); 
    String content = print_content(con); 
    JSONObject json = new JSONObject(content); 
    fileOutputStream1.write(json.toString().getBytes()); 
} catch (MalformedURLException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

그리고 여기 방법 :

private static String print_content(HttpsURLConnection con){ 
    if(con!=null){ 

     try { 

      BufferedReader br = 
        new BufferedReader(
          new InputStreamReader(con.getInputStream())); 

      String input; 
      String data = ""; 

      while ((input = br.readLine()) != null){ 
       data = data + input; 
      } 
      br.close(); 
      return data; 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
    return ""; 

} 

내가 연결에서 올바른 텍스트를 얻을 것을 확신은, 내가 전에 그것을 밖으로 인쇄. 당신은 꽤 잭슨을 사용하여 인쇄 할 수 있습니다 https://apps.runescape.com/runemetrics/profile/profile?user=Gragoyle&activities=20

+0

앱을 디버그하고 동일한 json 응답을 얻었습니까? – csanonymus

+0

이상하게도 디버그 모드에서 작동합니다. 그러나 그것은 그것을 prettyprint하지 않습니다, 그것은 한 줄로 돌아옵니다. 왜 그런 일이 일어날 지 알고 있습니까? –

+0

json을 인쇄하는 브라우저 확장 프로그램이나 뭔가있을 수 있지만 json은 일반적으로 한 줄짜리 프로그램입니다. 콘솔이나 로그에서 간단히 인쇄 할 수는 없습니다. – csanonymus

답변

0

:

다음은 httpsconnection의 링크입니다.

ObjectMapper mapper = new ObjectMapper(); 
String jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectWithSchemaOfJson); 
System.out.println(jsonInString); 

Pls는 확인 : 예쁜 프린트가 웹 페이지에 대해 필요한 경우 http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/

, <pre> 태그 내부에 JSON을 표시합니다.