我正在尝试连接下面的网址。这工作正常,但应用程序在执行后崩溃,有什么想法吗?Logcat 抱怨第 77 行 -由于 url 没有被执行connection = (HttpURLConnection)url.getContent();
,我改变了它。connection = (HttpURLConnection)url.openConnection();
此代码在 onClick Listener 按钮内执行。提前致谢。
URL url = null;
try {
url = new URL("http://apps.smilemachine.com/smilefactory/api/v1.0/speedup");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection)url.getContent();
} catch (IOException e) {
e.printStackTrace();
}
try {
connection.setRequestMethod("GET");
} catch (ProtocolException e) {
e.printStackTrace();
}
try {
connection.connect();
} catch (IOException e) {
e.printStackTrace();
}
日志猫输出:
11-28 22:11:21.153: E/AndroidRuntime(2676): 致命异常: main 11-28 22:11:21.153: E/AndroidRuntime(2676): java.lang.ClassCastException: java.util.zip.GZIPInputStream 11 -28 22:11:21.153: E/AndroidRuntime(2676): 在 com.developer.machine.Screen1$1.onClick(Screen1.java:81) 11-28 22:11:21.153: E/AndroidRuntime(2676): 在android.view.View.performClick(View.java:2485) 11-28 22:11:21.153: E/AndroidRuntime(2676): 在 android.view.View$PerformClick.run(View.java:9080) 11-28 22:11:21.153: E/AndroidRuntime(2676): 在 android.os.Handler.handleCallback(Handler.java:587) 11-28 22:11:21.153: E/AndroidRuntime(2676): 在 android.os.Handler .dispatchMessage(Handler.java:92) 11-28 22:11:21.153: E/AndroidRuntime(2676): 在 android.os.Looper.loop(Looper.java:123) 11-28 22:11:21.153: E /AndroidRuntime(2676):在 android.app.ActivityThread。main(ActivityThread.java:3683) 11-28 22:11:21.153: E/AndroidRuntime(2676): at java.lang.reflect.Method.invokeNative(Native Method) 11-28 22:11:21.153: E/AndroidRuntime (2676): 在 java.lang.reflect.Method.invoke(Method.java:507) 11-28 22:11:21.153: E/AndroidRuntime(2676): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:839)11-28 22:11:21.153:E/AndroidRuntime(2676):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)11-28 22:11: 21.153:E/AndroidRuntime(2676):在 dalvik.system.NativeStart.main(本机方法)android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 11-28 22:11:21.153: E/AndroidRuntime(2676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :597) 11-28 22:11:21.153: E/AndroidRuntime(2676): at dalvik.system.NativeStart.main(Native Method)android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 11-28 22:11:21.153: E/AndroidRuntime(2676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :597) 11-28 22:11:21.153: E/AndroidRuntime(2676): at dalvik.system.NativeStart.main(Native Method)