##代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void getOkhttp() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(str_log)//str_log:服务器地址
.build();
try {
Response response = client.newCall(request).execute();//同步,不可在主线程调用
str = response.body().string();//str:空字符串
Toast.makeText(this, str + "", Toast.LENGTH_SHORT).show();//吐司
Log.e("TAG" , str + "") ;//日志
} catch (IOException e) {
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void postOkhttp(){
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8") ;
OkHttpClient client = new OkHttpClient() ;
//str_json:参数 ex:{"username":"xxx","password":"xxx"}。。。。
RequestBody body = RequestBody.create(JSON , str_json) ;
Request request = new Request.Builder()
.url(str_url + "方法名")//str_url:服务器地址
.post(body)
.build();
try {
Response response = client.newCall(request).execute() ;//同步,不可主线程调用
Log.e("TAG" , response.body().string() + "") ;
} catch (IOException e) {
e.printStackTrace();
}
}