android 23 httpclient 替代,我需要Android中HttpClient的替代选项来将数据发送到PHP,因为不再支持...

4984055e0c68d4aa36a69e72b377edad.png

holdtom

我也遇到了这个问题,以解决自己上的课。哪个基于java.net,并且支持高达android的API 24,请检出: HttpRequest.java使用此类,您可以轻松地:发送Http GET请求发送Http POST请求发送Http PUT请求发送Http DELETE发送没有额外数据参数的请求并检查响应 HTTP status codeHTTP Headers向请求添加自定义(使用varargs)添加数据参数作为String查询请求将数据参数添加为HashMap{key = value}接受为 String接受为 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 workedreq.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 HashMapparams=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 AsyncTasknew AsyncTask(){        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 sendString 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 PUTreq.withData(json);//Add json data to request bodyJSONObject 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 sendString url="http://some.url";//URL address where we need to send it //Shortcut for example 5 complex request sending & reading response in one (chained) lineJSONObject res=new HttpRequest(url).withHeaders("Content-Type: application/json").prepare(HttpRequest.Method.PUT).withData(json).sendAndReadJSON();示例7://Downloading filebyte [] file = new HttpRequest("http://some.file.url").prepare().sendAndReadBytes();FileOutputStream fos = new FileOutputStream("smile.png");fos.write(file);fos.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值