ani*_*bet 68 post android http android-emulator
我正在使用Android SDK 2.2,使用模拟器测试我的应用程序.我想发送HTTP帖子.当我这样做时,我得到一个UnknownHostException.我已经把所需的权限
<uses-permission android:name="android.permission.INTERNET" />
在manifest.xml.此外,我可以在模拟器上打开浏览器并导航到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 );
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*k V 51
为了别人的考虑,我遇到了这个问题,谷歌让我着迷.正如anisbet所提到的,我仔细检查了我的许可标签,它位于正确的位置.
我最终启动了内置浏览器的android,并从我的网络服务器和Google.com获得相同的响应(虽然计算机本身很好).我终止了android模拟器并重新启动; 第一次尝试工作.
在查看代码之后,重启模拟器可能是值得的.在对模拟器完全公平的情况下,一堆程序在执行此操作后不久就崩溃了,所以我的计算机中可能还有其他东西在发生.尽管如此,这给我浪费了大量的时间,所以也许这会让别人感到头疼.
祝你好运!
igo*_*igo 33
确保您有互联网连接.当我忘记我正在使用没有互联网连接的手机进行测试时,发生在我身上的事情发生了.
Mor*_*itz 17
你知道是什么解决了它是在关闭清单标记之前放置权限,如下所示:
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57111 次 |
| 最近记录: |