如何使用HttpURLConnection发布数据

时间:2016-04-11 00:56:08

标签: android http-post httpclient httpurlconnection

我已经从API23更改为22,因为他们说httpclient不可用。当我切换到API22时,我遇到了HttpClient,HttpPost和NameValuePair的问题。我找到了使用HttpURLConnectionHandler的解决方案。但我不知道如何将它用于以下方法。

 public void send(View v)
{

    HttpClient httpclient = new DefaultHttpClient();
    // put the address to your server and receiver file here
    HttpPost httppost = new HttpPost("http://yoursite/yourPHPScript.php");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        // we wont be receiving the parameter ID in your server, but it is here to show you how you can send more data
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        // message is the parameter we are receiving, it has the value of 1 which is the value that will be sent from your server to your Arduino board
        nameValuePairs.add(new BasicNameValuePair("message", "1"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httpclient.execute(httppost); // send the parameter to the server
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

有人帮助我

0 个答案:

没有答案