我需要一个选项,HttpClient的Android中发送数据到PHP,因为它是pcated在API 22日$ P $ [英] I need an option to HttpClient in Android to send data to PHP as it is deprecated in API 22
问题描述
目前我使用的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的依赖关系:<一href="http://search.maven.org/#artifactdetails|org.apache.commons|commons-io|1.3.2|jar">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
这篇关于我需要一个选项,HttpClient的Android中发送数据到PHP,因为它是pcated在API 22日$ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!