所以在Java中,我正在尝试执行 HTTP GET请求以从网页获取一些JSON数据。网页设置正确,它只是将JSON文件显示为常规文本:
{"checklist": [{"name": "Shutting Down Lab 37", "id": 2}, {"name": "Caging the Monkey", "id": 3}], "groupId": "1"}
如果我直接在浏览器中输入链接,那就是它现在出现的方式。
我想从HTTP请求中提取此数据字符串,仅用于测试System.out.println
。
我运行代码时收到FATAL EXCEPTION: Main
。请帮忙!
public void GetRequest() throws MalformedURLException, IOException {
String charset = "UTF-8";
URLConnection connection = new URL(webpage).openConnection();
InputStream response = connection.getInputStream();
String contentType = connection.getHeaderField("Content Type");
for (String param : contentType.replace(" ", "").split(";")) {
if (param.startsWith("charset=")) {
charset = param.split("=", 2)[1];
break;
}
}
if (charset != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(response, charset));
try {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}
finally {
try { reader.close(); } catch (IOException logOrIgnore) {}
}
}
else {
}
}
LogCat错误
12-22 21:44:41.976: E/AndroidRuntime(1659): FATAL EXCEPTION: main
12-22 21:44:41.976: E/AndroidRuntime(1659): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medusa.checkit/com.medusa.checkit.NewChecklistActivity}: android.os.NetworkOnMainThreadException
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.os.Looper.loop(Looper.java:137)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-22 21:44:41.976: E/AndroidRuntime(1659): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 21:44:41.976: E/AndroidRuntime(1659): at java.lang.reflect.Method.invoke(Method.java:511)
12-22 21:44:41.976: E/AndroidRuntime(1659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-22 21:44:41.976: E/AndroidRuntime(1659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-22 21:44:41.976: E/AndroidRuntime(1659): at dalvik.system.NativeStart.main(Native Method)
12-22 21:44:41.976: E/AndroidRuntime(1659): Caused by: android.os.NetworkOnMainThreadException
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
12-22 21:44:41.976: E/AndroidRuntime(1659): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
12-22 21:44:41.976: E/AndroidRuntime(1659): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
12-22 21:44:41.976: E/AndroidRuntime(1659): at java.net.InetAddress.getAllByName(InetAddress.java:220)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
12-22 21:44:41.976: E/AndroidRuntime(1659): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
12-22 21:44:41.976: E/AndroidRuntime(1659): at com.medusa.checkit.ServerCall.GetRequest(ServerCall.java:22)
12-22 21:44:41.976: E/AndroidRuntime(1659): at com.medusa.checkit.NewChecklistActivity.onCreate(NewChecklistActivity.java:34)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.Activity.performCreate(Activity.java:4470)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-22 21:44:41.976: E/AndroidRuntime(1659): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-22 21:44:41.976: E/AndroidRuntime(1659): ... 11 more
答案 0 :(得分:0)
你应该得到一个nullpointer异常。将您的代码更改为以下内容以获取内容类型
connection.getHeaderField("Content-Type");
答案 1 :(得分:0)
问题是NetworkOnMainThreadException
,对于那些不知道的人,你不应该在主线程上运行任何网络调用。
我通过输入AsyncTask
来解决这个问题。
感谢@EJP指出这一点!