我需要一个选项,HttpClient的Andr​​oid中发送数据到PHP,因为它是pcated在API 22日$ P $它是、选项、数据、Andr

由网友(未知數旳等待)分享简介:目前我使用的HttpClient , HttpPost 将数据发送到我的 PHP服务器从 Android应用但这些所有的方法都pcated在API 22究竟什么是选择它去$ P $。我到处找,但我没有发现任何东西。Currently I'm using HttpClient, HttpPost to send dat...

目前我使用的HttpClient HttpPost 将数据发送到我的 PHP服务器 Android应用但这些所有的方法都pcated在API 22究竟什么是选择它去$ P $。我到处找,但我没有发现任何东西。

Currently I'm using HttpClient, HttpPost to send data to my PHP server from an Android app but those all methods are deprecated in API 22 so what is the option to it. I searched everywhere but I didn't find anything.

推荐答案

在 HttpClient的文件指向你在正确的方向:

The HttpClient documentation points you in the right direction:

org.apache.http.client.HttpClient

该接口是pcated在API层面22日$ P $。   请使用的openConnection()来代替。请访问此网页了解更多详情。

This interface was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.

意味着你应该切换到 java.net.URL.openConnection()

下面是你如何能做到这一点:

Here's how you could do it:

URL url = new URL("http://some-server");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

// read the response
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);

IOUtils 文档:的Apache下议院IO IOUtils Maven的依赖关系:http://search.maven.org/#artifactdetails|org.apache.commons|commons-io|1.3.2|jar

IOUtils documentation: Apache Commons IO IOUtils Maven dependency: http://search.maven.org/#artifactdetails|org.apache.commons|commons-io|1.3.2|jar

阅读全文

相关推荐

最新文章