这是一个简单的获取请求
我发现异常是由
引起的"HttpResponse response = client.execute(request);"
我不知道为什么,因为它是从示例中复制的。
我尝试了不同的网址,但仍然得到了例外
如果我打印异常,我将得到空指针异常
现在我不知道如何找出问题
有谁知道这个问题的原因是什么?
这是针对此问题的视频link -
public void onClick(View view) {
String url = "http://www.google.com";
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
} catch (Exception e) {
//Log.v("cc",e.getMessage());
Log.v("dd", "error");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="so.lab4"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Lab4Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
在事件线程中建立HTTP连接太差了!
你应该在工作线程中做这种工作。如果没有,您将暂停处理GUI事件的事件线程,如果线程保持超过5秒,它将抛出一个不错的ANR(AKA应用程序无响应)错误。