目前,我正在使用中HttpClient,HttpPost从中发送数据给我PHP server,Android app但是所有这些方法在API 22中已被弃用,而在API 23中已被删除,那么还有哪些替代选择呢?
HttpClient
HttpPost
PHP server
Android app
我到处搜索,但没有找到任何东西。
该HttpClient的已被废弃,现在删除:
org.apache.http.client.HttpClient:
该接口在API级别22中已弃用。请改用openConnection()。请访问此网页以获取更多详细信息。
表示你应该切换到java.net.URL.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
我也遇到了这个问题,以解决自己上的课。哪个基于java.net,并支持高达android的API 24,请检出: HttpRequest.java
使用此类,您可以轻松地:
GET
POST
PUT
DELETE
HTTP status code
HTTP Headers
String
HashMap{key = value}
JSONObject
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();