我需要在 Android 中使用 HttpClient 的替代选项将数据发送到 PHP,因为它不再受支持 [英] I need an alternative option to HttpClient in Android to send data to PHP as it is no longer supported
问题描述
目前我正在使用 HttpClient
、HttpPost
从 Android 应用程序
将数据发送到我的 PHP 服务器
但所有这些方法在 API 22 中都已弃用并在 API 23 中删除,那么它的替代选项是什么?
Currently I'm using HttpClient
, HttpPost
to send data to my PHP server
from an Android app
but all those methods were deprecated in API 22 and removed in API 23, so what are the alternative options to it?
我到处搜索,但没有找到任何东西.
I searched everywhere but I didn't find anything.
推荐答案
HttpClient 已弃用,现在已删除:
The HttpClient was deprecated and now removed:
org.apache.http.client.HttpClient
:
此接口已在 API 级别 22 中弃用.请改用 openConnection().请访问此网页了解更多详情.
This interface was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details.
意味着你应该切换到java.net.URL.openConnection()
.
另请参阅新的 HttpURLConnection 文档.
See also the new HttpURLConnection documentation.
您可以这样做:
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 Commons IOIOUtils
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
这篇关于我需要在 Android 中使用 HttpClient 的替代选项将数据发送到 PHP,因为它不再受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!