【发布时间】:2016-04-11 00:56:08
【问题描述】:
我从 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
}
}
谁来帮帮我
【问题讨论】:
-
您到底想做什么,您遇到的具体问题是什么? (您收到了哪些错误消息。)
-
HttpPost、HttpClient 和 NameValuePair 已弃用。我需要将此 post 方法与我已经拥有的 HttpURLConnectionHandler 类一起使用@ChrisBritt
标签: android http-post httpclient httpurlconnection