导包
//OKHttp
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.1'
compile 'com.squareup.okio:okio:1.13.0'
GET同步
//同步GET
new Thread() {
private OkHttpClient okHttpClient = new OkHttpClient();
@Override
public void run() {
super.run();
Log.i("imtest","开始执行");
Request request = new Request.Builder()
.url("http://184.200.73.1:8080/imsjw/user/test").build();
try {
Response response = okHttpClient.newCall(request).execute();
Log.i("imtest","执行结束");
Log.i("imtest",response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
GET异步
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://184.200.73.1:8080/imsjw/user/test").build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("imtest","OKHTTP异步错误");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("imtest",response.body().string());
}
});
异步POST提交表单
//异步POST提交表单
//异步POST提交表单
OkHttpClient okHttpClient = new OkHttpClient();
FormBody body = new FormBody.Builder().add("name", "nametest").add("pwd", "pwdtest").build();
Request request = new Request.Builder()
.url("http://184.200.73.1:8080/imsjw/user/test").post(body).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("imtest", "OKHTTP异步错误");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("imtest", response.body().string());
}
});
6万+

被折叠的 条评论
为什么被折叠?



