安卓的UnknownHostException [英] Android: UnknownHostException

查看:111
本文介绍了安卓的UnknownHostException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Andr​​oid SDK 2.2,我的测试与仿真应用程序。我想送一个HTTP帖子。当我做我得到一个UnknownHostException异常。我已经把所需的权限
<使用-权限的Andr​​oid:名称=android.permission.INTERNET对/ >
在manifest.xml。此外,我可以打开浏览器上的仿真器,然后导航到没有问题的URL。

下面是我的code:

  HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(URI);
HTT presponse响应= NULL;
尝试
{
//添加数据
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(
2);
nameValuePairs.add(新BasicNameValuePair(ID,编辑名));
namevaluepairs中
。新增(新BasicNameValuePair(StringData是,用户名));
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));

//执行HTTP POST请求
响应= httpclient.execute(httppost);
// Log.i(HttpManager:,==>回应:
// + response.getEntity()的getContent())。

}
赶上(ClientProtocolException E)
{
Log.e(HttpManager,ClientProtocolException抛出+ E);
}
赶上(IOException异常E)
{
Log.e(HttpManager,抛出的IOException+ E);
}
 

解决方案

好吧,我觉得pretty的跛脚...... Internet权限标签是明显的标记,而不是应用程序标签的孩子。 Sheesh!

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.

Here is my code:

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 );
}

解决方案

Ok, I feel pretty lame... The INTERNET permission tag is a child of the manifest tag, not the application tag. Sheesh!

这篇关于安卓的UnknownHostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆