一尘不染

我需要Android中HttpClient的另一个选项来向PHP发送数据,因为它不再受支持

java

目前,我正在使用中HttpClientHttpPost从中发送数据给我PHP serverAndroid app但是所有这些方法在API 22中已被弃用,而在API 23中已被删除,那么还有哪些替代选择呢?

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


阅读 346

收藏
2020-03-24

共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);
IOUtils文档:Apache Commons IO

IOUtils Maven依赖项:http : //search.maven.org/#artifactdetails|org.apache.commons|commons-io| 1.3.2| jar

2020-03-24
一尘不染

我也遇到了这个问题,以解决自己上的课。哪个基于java.net,并支持高达android的API 24,请检出: HttpRequest.java

使用此类,您可以轻松地:

  1. 发送Http GET请求
  2. 发送Http POST请求
  3. 发送Http PUT请求
  4. 发送Http DELETE
  5. 发送请求而无需额外的数据参数并检查响应 HTTP status code
  6. HTTP Headers向请求添加自定义(使用varargs)
  7. 添加数据参数作为String查询请求
  8. 将数据参数添加为HashMap{key = value}
  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();
2020-03-24