我正在使用 Android SDK 2.2,使用模拟器测试我的应用程序.我想发送一个 HTTP Post.当我这样做时,我得到一个 UnknownHostException.我已放置所需的权限
在 manifest.xml 中.我也可以在模拟器上打开浏览器并毫无问题地导航到 URL.
I am using Android SDK 2.2, testing my application with the emulator. I want to send a HTTP Post. When I do I get a UnknownHostException. I have placed the required permissions
<uses-permission android:name="android.permission.INTERNET" /
>
in the manifest.xml. Also I can open the browser on the emulator and navigate to the URL with no problem.
这是我的代码:
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 );
}
INTERNET 权限标签是清单标签的子标签,而不是应用标签.
The INTERNET permission tag is a child of the manifest tag, not the application tag.
这篇关于安卓:未知主机异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!