我正在使用Android SDK 2.2,并在模拟器上测试我的应用程序。我想发送一个HTTP Post。当我得到一个UnknownHostException。我已经在manifest.xml中放置了所需的权限
<uses-permission android:name="android.permission.INTERNET" /
>
。另外,我可以在模拟器上打开浏览器,然后毫无问题地导航到URL。
这是我的代码:
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 );
}
} catch(Exception e) { System.out.println("Error:"+e); } catch (ClientProtocolException e) { Log.e( "HttpManager", "ClientProtocolException thrown" + e ); } catch (IOException e) { Log.e( "HttpManager", "IOException thrown" + e ); }
这表明错误已被视为已处理的异常。