okhttp使用

准备工作:
1、导入okhttp-2.7.5.jar和okio-1.11.0.jar
2、权限设置
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
一、post请求:

public void getData(String url){
new Thread(new Runnable() {
@Override
public void run() {
try {
String s="";//入参 json形式
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);
Request request = new Request.Builder()
.post(body)
.url(url).
build();
Response response = null;
response = client.newCall(request).execute();//得到Response 对象
if (response.isSuccessful()) {
Log.d("response.code()==",response.code()+"");
Log.d("response.message()==",response.message());
Log.d("res==",response.body().string());

}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
运行结果:

D/response.code()==: 200
D/response.message()==: OK
D/res==: {"result":"ok","airTemperature":19,"airTempMaxValue":25,"airTempMinValue":18,"airHumidity":58,"airHumiMaxValue":25,"airHumiMinValue":18,"earthTemperature":14,"earthTempMaxValue":25,"earthTempMinValue":18,"earthHumidity":3,"earthHumiMaxValue":25,"earthHumiMinValue":18,"light":70,"lightMaxValue":25,"lightMinValue":18,"co2":77,"co2MaxValue":25,"co2MinValue":18}


二、get请求
public void getDataByGet(String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.get()
.url(url)
.build();
//通过client发起请求
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {

}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
str = response.body().string();
Log.d("=====", str);
}
}
});
}

运行结果:

D/=====: {"RESULT": "S", "data": [{"stop": false, "speedmin": 0, "carplate": "宝马", "carno": "辽A10001", "$min": 0, "$max": 10000, "amount": 1653.0, "speedmax": 1000, "who": "张三", "speed": 100}, {"stop": false, "speedmin": 0, "carplate": "中华", "carno": "辽A10002", "$min": 0, "$max": 10000, "amount": 1162.0, "speedmax": 1000, "who": "李四", "speed": 100}, {"stop": false, "speedmin": 0, "carplate": "奔驰", "carno": "辽A10003", "$min": 0, "$max": 10000, "amount": 143.0, "speedmax": 1000, "who": "王五", "speed": 100}, {"stop": false, "speedmin": 0, "carplate": "马自达", "carno": "辽A10004", "$min": 0, "$max": 10000, "amount": 21.0, "speedmax": 1000, "who": "赵六", "speed": 100}, {"stop": false, "speedmin": 0, "carplate": "斯柯达", "carno": "辽A10005", "$min": 0, "$max": 10000, "amount": 0, "speedmax": 1000, "who": "上官六", "speed": 100}], "ERRMSG": "成功"}

三、异步

1.异步请求,通过接口回调告知用户 http 的异步执行结果
//通过client发起请求
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {

}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
str = response.body().string();
Log.d("=====", str);
}
}
});


2、同步请求
if (response.isSuccessful()) {
Log.d("response.code()==", response.code() + "");
Log.d("response.message()==", response.message());
Log.d("res==", response.body().string());

}

思考:post方法的写法,是放入线程中的,可以考虑实现两点
1、模仿上面get方式,网络访问不放入线程中的写法
2、post用异步访问的方式写
 

 

posted on 2018-10-25 15:53  谌桂枝  阅读(462)  评论(0编辑  收藏  举报