工具类的单例
1.工具类的单例----》client对象
private OkHttpClient okHttpClient;
//构造私有化
private Okhttputils(){
okHttpClient=new OkHttpClient.Builder()
.writeTimeout(60*1000, TimeUnit.MILLISECONDS)
.readTimeout(60*1000, TimeUnit.MILLISECONDS)
.connectTimeout(60*1000, TimeUnit.MILLISECONDS)
.build();
}
//私有静态实例化
private static Okhttputils okhttputils=new Okhttputils();
//提供一个公开静态方法
public static Okhttputils getInstance(){
if (okhttputils==null){
okhttputils=new Okhttputils();
}
return okhttputils;
}
Get方法
MainActivity中启动Get方法
//用工具类提供的方法获取构造
Okhttputils instance = Okhttputils.getInstance();
instance.doGet("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1", new OkhttpCallback() {
@Override
public void onOk(final String json) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast.makeText(MainActivity.this, ""+json, Toast.LENGTH_SHORT).show();
}
});
}
//Get方法
instance.doGet("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1", new OkhttpCallback() {
@Override
public void onOk(final String json) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast.makeText(MainActivity.this, ""+json, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(final String message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, ""+message, Toast.LENGTH_SHORT).show();
}
});
}
});
Post中的FormBody
MainActivity中启动Post方法
HashMap<String, String> map = new HashMap<>();
map.put("name","aaaa");
map.put("password","123456");
instance.doPost("http://49.233.0.68:8080/register", map, new OkhttpCallback() {
@Override
public void onOk(final String json) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, ""+json, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(String message) {
}
});
public void doPost(String url, HashMap<String,String> map, final OkhttpCallback okhttpCallback){
FormBody.Builder builder = new FormBody.Builder();
Set<String> keySet = map.keySet();
for (String key:keySet){
String values = map.get(key);
builder.add(key,values);
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.post(formBody)
.url(url)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String message = e.getMessage();
okhttpCallback.onError(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
okhttpCallback.onOk(string);
}
});
}
Post中的RequestBody
MainActivity中启动Post方法
instance.doPostjson("http://49.233.0.68:8080/searchAround", json, new OkhttpCallback() {
@Override
public void onOk(final String json) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, ""+json, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(final String message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, ""+message, Toast.LENGTH_SHORT).show();
}
});
}
});
//Post中的RequestBody
public void doPostjson(String url, String json, final OkhttpCallback okhttpCallback){
MediaType parse = MediaType.parse("application/json;charset=utf-8");
RequestBody requestBody = RequestBody.create(parse, json);
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String message = e.getMessage();
okhttpCallback.onError(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
okhttpCallback.onOk(string);
}
});
拦截器
log拦截器写在最后
log拦截器
加入依赖---->implementation ‘com.squareup.okhttp3:logging-interceptor:3.4.1’
//log拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// 在client中
.addInterceptor(httpLoggingInterceptor)//log拦截器
自定义拦截器
//自定义拦截器
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("appVersion", "1.0").build();
return chain.proceed(request);
}
};
//在client中
.addInterceptor(interceptor)//自定义拦截器