[英]org.json.JSONException: End of input at character 0 of
我想在Android中漂亮地打印JSON文本。 我的問題是我得到了錯誤
輸入的結束字符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 "";
}
我確信我從連接中得到了正確的文本,我之前已經打印了出來。
這是httpsconnection的鏈接: https ://apps.runescape.com/runemetrics/profile/profile ? user = Gragoyle & activities =20
您可以使用Jackson進行漂亮的打印。
ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectWithSchemaOfJson);
System.out.println(jsonInString);
請檢查: http : //www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/
如果網頁需要漂亮的打印件,請在<pre>
標簽內顯示JSON。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.