问题描述
目前我使用的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()来代替。请访问此网页了解更多详情。
意味着你应该切换到 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 = .io.IOUtils.toString(in, "UTF-8");
System.out.println(response);
IOUtils
文档:的Apache下议院IO IOUtils
Maven的依赖关系:||||http://search.maven.org/#artifactdetails||||jar
IOUtils
documentation: Apache Commons IOIOUtils
Maven dependency: http://search.maven.org/#artifactdetails||||jar
这篇关于我需要一个选项,HttpClient的Android中发送数据到PHP,因为它是pcated在API 22日$ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!