我正在尝试连接下面的网址。这工作正常但应用程序在执行后崩溃,任何想法? Logcat抱怨第77行 - 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();
}
Log cat输出:
11-28 22:11:21.153:E / AndroidRuntime(2676):致命异常:主要 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):at 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):at java.lang.reflect.Method.invoke(Method.java:507) 11-28 22:11:21.153:E / AndroidRuntime(2676):at com.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)