Android下通过HttpClient执行 HTTP POST 请求

chongq 分享于 5天前 1337阅 0人收藏此代码, 我要收藏

Android下通过HttpClient执行 HTTP POST 请求

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

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

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

//该代码片段来自于: http://www.sharejs.com/codes/java/8913

标签:
  • Android
  • HttpClient
  • POST