【发布时间】:2010-07-22 22:50:44
【问题描述】:
我正在使用 Android SDK 2.2,使用模拟器测试我的应用程序。我想发送一个 HTTP Post。当我这样做时,我得到一个 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 );
}
【问题讨论】:
-
} 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 ); }这显示错误表示已处理异常。
标签: android http post android-emulator