我正在使用Android SDK 2.2,使用模擬器測試我的應用程序。我想發送一個HTTP Post。當我做我得到一個UnknownHostException。我已經在manifest.xml中放置了所需的權限
<uses-permission android:name="android.permission.INTERNET" /
>
。此外,我可以在模擬器上打開瀏覽器並導航到URL,沒有問題。Android:UnknownHostException
這裏是我的代碼:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri);
HttpResponse response = null;
try
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("id", "edit-name"));
nameValuePairs
.add(new BasicNameValuePair("stringdata", userName));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
// Log.i("HttpManager:", "======> response: "
// + response.getEntity().getContent());
}
catch (ClientProtocolException e)
{
Log.e("HttpManager", "ClientProtocolException thrown" + e);
}
catch (IOException e)
{
Log.e("HttpManager", "IOException thrown" + e);
}
'} \t \t \t趕上(例外五) \t \t \t { \t \t \t \t的System.out.println( 「錯誤:」 + e)條; \t \t \t} \t \t趕上(ClientProtocolException E) \t \t { \t \t Log.e( 「HttpManager」, 「ClientProtocolException拋出」 + E); \t \t} \t \t趕上(IOException的E) \t \t { \t \t Log.e( 「HttpManager」, 「IOException的拋出」 + E); \t \t}' This顯示錯誤說已處理異常。 – 2016-06-25 18:50:35