您的位置:首页 > 理论基础 > 计算机网络

OkHttp框架加拦截器

2017-10-26 10:15 357 查看
拦截器需要添加依赖:

compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
OKhttp框架依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'


代码

public class OkHttpUtils {

//声明客户端
private OkHttpClient client;
//防止多个线程同时访问所造成的安全隐患
private volatile static OkHttpUtils utils;
private Context context;
//子线程
private Handler handler;
//构造方法
private OkHttpUtils(Context context){
//拦截器
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
client=new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
handler=new Handler(Looper.getMainLooper());
this.context=context;
}
//单例模式
public static OkHttpUtils getInstance(Context context){
if(utils==null){
//线程同步锁
synchronized (OkHttpUtils.class){
if(utils==null){
utils=new OkHttpUtils(context);
utils=utils;
}
}
}
return utils;
}

//网络请求
//syncJsonStringByUrl
public void doGet(String url, final FuncJsonString callback){
//网络判断
if (!NetWorkUtil.isNetworkAvailable(context)) {
Toast.makeText(context, "没有网络,请查看设置", Toast.LENGTH_SHORT).show();
return;
}
Request request=new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("TAG", "解析失败");
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if(response!=null&&response.isSuccessful()){
OnSuccessJsonString(response.body().string(),callback);
}
}
});
}

//post提交表单数据
//sendDataForClicent
public void dopost(String url, Map<String,String> params, final FuncJsonString callback){
//网络判断
if (!NetWorkUtil.isNetworkAvailable(context)) {
Toast.makeText(context, "没有网络,请查看设置", Toast.LENGTH_SHORT).show();
return;
}
//表单对象,包含input开始的操作
FormBody.Builder from=new FormBody.Builder();
if(params != null && !params.isEmpty()){
for (Map.Entry<String,String> entry:params.entrySet()){
//装载表单值
from.add(entry.getKey(),entry.getValue());
}
}
RequestBody body=from.build();
//post提交
Request request=new Request.Builder().url(url).post(body).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("TAG", "解析失败");
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if(response!=null&&response.isSuccessful()){
OnSuccessJsonString(response.body().string(),callback);
}
}
});
}

//返回json字符串的接口
public interface FuncJsonString{
void onResponse(String result);
}
//请求的返回是json字符串
private void OnSuccessJsonString(final String jsonValue, final FuncJsonString callback){
handler.post(new Runnable() {
@Override
public void run() {
if(callback!=null){
try {
callback.onResponse(jsonValue);
}catch (Exception e){

}
}
}
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: