我需要Android中HttpClient的替代选项将数据发送到PHP,因为它不再受支持

目前我正在使用 ,从 一个 向我发送数据,但所有这些方法在 API 22 中已被弃用,在 API 23 中被删除,那么它的替代选项是什么?HttpClientHttpPostPHP serverAndroid app

我到处搜索,但我没有找到任何东西。


答案 1

我也遇到过这个问题,要解决,我已经做了自己的类。哪个基于 java.net,并支持Android的API 24,请查看:HttpRequest.java

使用此类,您可以轻松:

  1. 发送 Http 请求GET
  2. 发送 Http 请求POST
  3. 发送 Http 请求PUT
  4. 发送DELETE
  5. 发送请求而无需额外的数据参数和检查响应HTTP status code
  6. 将自定义添加到请求(使用 varargs)HTTP Headers
  7. 将数据参数添加为查询以请求String
  8. 将数据参数添加为 {key=value}HashMap
  9. 接受响应为String
  10. 接受响应为JSONObject
  11. 接受响应为字节数组(对文件有用)byte []

以及这些的任意组合 - 只需一行代码)

以下是一些示例:

//Consider next request: 
HttpRequest req=new HttpRequest("http://host:port/path");

示例 1

//prepare Http Post request and send to "http://host:port/path" with data params name=Bubu and age=29, return true - if worked
req.prepare(HttpRequest.Method.POST).withData("name=Bubu&age=29").send();

示例 2

// prepare http get request,  send to "http://host:port/path" and read server's response as String 
req.prepare().sendAndReadString();

示例 3

// prepare Http Post request and send to "http://host:port/path" with data params name=Bubu and age=29 and read server's response as JSONObject 
HashMap<String, String>params=new HashMap<>();
params.put("name", "Groot"); 
params.put("age", "29");
req.prepare(HttpRequest.Method.POST).withData(params).sendAndReadJSON();

示例 4

//send Http Post request to "http://url.com/b.c" in background  using AsyncTask
new AsyncTask<Void, Void, String>(){
        protected String doInBackground(Void[] params) {
            String response="";
            try {
                response=new HttpRequest("http://url.com/b.c").prepare(HttpRequest.Method.POST).sendAndReadString();
            } catch (Exception e) {
                response=e.getMessage();
            }
            return response;
        }
        protected void onPostExecute(String result) {
            //do something with response
        }
    }.execute(); 

示例 5

//Send Http PUT request to: "http://some.url" with request header:
String json="{\"name\":\"Deadpool\",\"age\":40}";//JSON that we need to send
String url="http://some.url";//URL address where we need to send it 
HttpRequest req=new HttpRequest(url);//HttpRequest to url: "http://some.url"
req.withHeaders("Content-Type: application/json");//add request header: "Content-Type: application/json"
req.prepare(HttpRequest.Method.PUT);//Set HttpRequest method as PUT
req.withData(json);//Add json data to request body
JSONObject res=req.sendAndReadJSON();//Accept response as JSONObject

示例 6

//Equivalent to previous example, but in a shorter way (using methods chaining):
String json="{\"name\":\"Deadpool\",\"age\":40}";//JSON that we need to send
String url="http://some.url";//URL address where we need to send it 
//Shortcut for example 5 complex request sending & reading response in one (chained) line
JSONObject res=new HttpRequest(url).withHeaders("Content-Type: application/json").prepare(HttpRequest.Method.PUT).withData(json).sendAndReadJSON();

示例 7

//Downloading file
byte [] file = new HttpRequest("http://some.file.url").prepare().sendAndReadBytes();
FileOutputStream fos = new FileOutputStream("smile.png");
fos.write(file);
fos.close();

答案 2

HttpClient 已被弃用,现在已被删除:

org.apache.http.client.HttpClient:

此接口在 API 级别 22 中已弃用。请改用 openConnection()。请访问此网页了解更多详情。

表示您应该切换到 。java.net.URL.openConnection()

另请参阅新的 HttpURLConnection 文档。

以下是您可以执行的操作:

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);

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


推荐