對不起,我是新來的世界上的java和android。 我想了解爲什麼它總是響應錯誤。 謝謝!Android HTTP GET錯誤
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://www.google.com";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
//e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
進口
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
,是的,我進入許可互聯網
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testhttp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
使用logcat的檢查你的「錯誤」相關的Java堆棧跟蹤。如果您不瞭解堆棧跟蹤,請編輯您的問題並將其發佈到此處。 – CommonsWare
取消註釋'e.printStackTrace()'並查看logcat中的異常 – Navid777
請參閱http://stackoverflow.com/questions/22395417/error-strictmodeandroidblockguardpolicy-onnetwork – user4805969