@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button3:
new Thread(new Runnable() {
@Override
public void run() {
Cache cache = new Cache(getCacheDir(), 8L * 1024L * 1024L);
OkHttpClient client = new OkHttpClient.Builder().cache(cache).addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response proceed = chain.proceed(request);//继续发起网络请求
proceed = proceed.newBuilder().removeHeader("Pragma").removeHeader("Cache-Control")
.addHeader("Cache-Control", "max-age=" + 20000).build();
return proceed;
}
}).build();
Request request = new Request.Builder().url(url).get().build();
Call call = client.newCall(request);
try {
Response execute = call.execute();
String s = execute.body().string();
Log.e("MainActivity", s+"");
Message message = new Message();
message.what=1;
message.obj=s;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.button4:
OkHttpClient client = new OkHttpClient.Builder().build();
final Request request = new Request.Builder().url(url).get().build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
Message message = new Message();
message.what=1;
message.obj=s;
handler.sendMessage(message);
}
});
break;
case R.id.button5:
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient.Builder().build();
FormBody body = new FormBody.Builder().add("scope", "103").add("format","json").add("appid","379020").add("bk_key","关键字").add("bk_length","600").build();
Request request = new Request.Builder().url(url).post(body).build();
Call call = client.newCall(request);
try {
Response execute = call.execute();
String s = execute.body().string();
Message message = new Message();
message.what=1;
message.obj=s;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.button6:
OkHttpClient build = new OkHttpClient.Builder().build();
FormBody body = new FormBody.Builder().add("scope", "103").add("format","json").add("appid","379020").add("bk_key","关键字").add("bk_length","600").build();
Request request1 = new Request.Builder().url(url_post).post(body).build();
Call call1 = build.newCall(request1);
call1.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Message message = new Message();
message.obj=string;
message.what=1;
handler.sendMessage(message);
}
});
break;
}
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button3:
new Thread(new Runnable() {
@Override
public void run() {
Cache cache = new Cache(getCacheDir(), 8L * 1024L * 1024L);
OkHttpClient client = new OkHttpClient.Builder().cache(cache).addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response proceed = chain.proceed(request);//继续发起网络请求
proceed = proceed.newBuilder().removeHeader("Pragma").removeHeader("Cache-Control")
.addHeader("Cache-Control", "max-age=" + 20000).build();
return proceed;
}
}).build();
Request request = new Request.Builder().url(url).get().build();
Call call = client.newCall(request);
try {
Response execute = call.execute();
String s = execute.body().string();
Log.e("MainActivity", s+"");
Message message = new Message();
message.what=1;
message.obj=s;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.button4:
OkHttpClient client = new OkHttpClient.Builder().build();
final Request request = new Request.Builder().url(url).get().build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
Message message = new Message();
message.what=1;
message.obj=s;
handler.sendMessage(message);
}
});
break;
case R.id.button5:
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient.Builder().build();
FormBody body = new FormBody.Builder().add("scope", "103").add("format","json").add("appid","379020").add("bk_key","关键字").add("bk_length","600").build();
Request request = new Request.Builder().url(url).post(body).build();
Call call = client.newCall(request);
try {
Response execute = call.execute();
String s = execute.body().string();
Message message = new Message();
message.what=1;
message.obj=s;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
break;
case R.id.button6:
OkHttpClient build = new OkHttpClient.Builder().build();
FormBody body = new FormBody.Builder().add("scope", "103").add("format","json").add("appid","379020").add("bk_key","关键字").add("bk_length","600").build();
Request request1 = new Request.Builder().url(url_post).post(body).build();
Call call1 = build.newCall(request1);
call1.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Message message = new Message();
message.obj=string;
message.what=1;
handler.sendMessage(message);
}
});
break;
}
}