Android通过HttpClient执行post请求的代码

151 阅读1分钟

将做工程过程中经常用的代码珍藏起来,下面资料是关于Android通过HttpClient执行post请求的代码。

public void postData() { HttpClient httpclient = new DefaultHttpClient();

try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    
} catch (ClientProtocolException e) {
} catch (IOException e) {
}

}