[英]android JSON from Url
So I am trying to retrieve JSON object from url. 所以我试图从URL检索JSON对象。 And I cannot retrieve anything from url. 而且我无法从url检索任何内容。 So to test why, I tried the same code on Spring application, and it works just fine. 因此,为了测试原因,我在Spring应用程序上尝试了相同的代码,并且工作正常。 It crashes on code below only on android environment. 它仅在android环境下在以下代码上崩溃。 Please help 请帮忙
package com.example.sandbox;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
}
Please check your internet connection and also check if you have added permission for INTERNET in your app's manifest that might be issue. 请检查您的互联网连接,并检查您是否在应用清单中添加了对INTERNET的许可,这可能是有问题的。
what error do you get exactly ? 您究竟会得到什么错误?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.