okHttpUtils工具类->get+post+拦截器

1.okHttpUtils工具类package com.example.gjl.day01_okhttputils.utils;import android.os.Handler;import android.os.Message;import android.util.Log;import java.io.File;import java.io.IOException;import jav
摘要由CSDN通过智能技术生成

1.okHttpUtils工具类

package com.example.gjl.day01_okhttputils.utils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 单利模式
 * 就是一个应用里面只有一个对象
 * 好处:节约内存。
 * 1.饿汉式
 * 开始对象就创建出来了
 * 2.懒汉式
 * 开始对象是null,什么时候使用,就是什么时候创建。
 */

public class OKHttpUtils {
    private static final String TAG = "OKHttpUtils----";

    private static OKHttpUtils okHttpUtils = null;

    private MyHandler myHandler = new MyHandler();
    private LoginListener onLoginListener;

    public static OKHttpUtils getInstance() {
        if (okHttpUtils == null) {
            okHttpUtils = new OKHttpUtils();
        }
        return okHttpUtils;
    }

    //get
    public void okGet(String url, String mobile, String password) {
        String url1 = url + "?mobile=" + mobile + "&password=" + password;
        //创建OKHttpClient
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build();
        //用request 请求,将url,和参数惊醒封装。
        Request request = new Request.Builder().url(url1).build();
        //call是请求队列,ok里面,请求队队列默认是一个。
        Call call = client.newCall(request);
        //一部请求,成功和失败的方法都是在线程,不能进行UI跟新。
        //handler   synctask
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure() returned: " + "失败--");
                Message message = myHandler.obtainMessage();
                message.what = 0;
                message.obj = e.getMessage();
                myHandler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                java.lang.IllegalStateException: closed
//                Log.d(TAG, "onResponse() returned: " + response.body().string());
                Message message = myHandler.obtainMessage();
                message.what = 1;
                message.obj = response.body().string();
                myHandler.sendMessage(message);
            }
        });
    }

    //post
    public void okPost(String url, Map<String, String> params) {
        //创建OKHttpClient
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build();
        FormBody.Builder builder = new FormBody.Builder();
        //遍历map集合,将参数放入body即可    遍历的核心思想就是。根据键取值
        Set<String> keys = params.keySet();
        for (String key : keys) {
            String value = params.get(key);
            //放入body
            builder.add(key, value);
        }

        FormBody body = builder.build();
        //用request 请求,将url,和参数惊醒封装。
        Request request = new Request.Builder().url(url).post(body).build();
        //call是请求队列,ok里面,请求队队列默认是一个。
        Call call = client.newCall(request);
        //一部请求,成功和失败的方法都是在线程,不能进行UI跟新。
        //handler   synctask
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure() returned: " + "失败--");
                Message message = myHandler.obtainMessage();
                message.what = 0;
                message.obj = e.getMessage();
                myHandler.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.d(TAG, "onResponse() returned: " + response.body().string());
                Message message = myHandler.obtainMessage();
                message.what = 1;
                message.obj = response.body().string();
                myHandler.sendMessage(message);
            }
        });
    }
    //上传

    //将输入发送到主线程,处理线程问题
    //接口回调
    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            int w = msg.what;
            switch (w) {
                case 0:
                    //失败
                    String error = (String) msg.obj;
                    //传出去
                    onLoginListener.loginError(error);
                    break;

                case 1:
                    //成功
                    String json = (String) msg.obj;
                    onLoginListener.loginSuccess(json);
                    break;
            }
        }
    }

    //
    public interface LoginListener {
        //成功
        void loginSuccess(String json);

        //失败
        void loginError(String error);
    }

    //提供一个外部访问的方法
    public void setOnLoginListener(LoginListener onLoginListener) {
        this.onLoginListener = onLoginListener;
    }

    //拦截器。实现公参数的拼接
    class MyIntercepter implements Interceptor {
        //拦截的方法。
//        chain链条,获取请求,获取相应
        @Override
        public Response intercept(Chain chain) throws IOException {
            //获取请求
            Request request = chain.request();
            RequestBody body = request.body();
            //判断如果是formbody
            if (body instanceof FormBody) {
                //创建新的body,将原来的参数,放入新的body里面,然后再讲公共参数,添加到新的body里面
                FormBody.Builder builder = new FormBody.Builder();
                //遍历旧的body
                for (int i = 0; i < ((FormBody) body).size(); i++) {
                    String key = ((FormBody) body).name(i);
                    String value = ((FormBody) body).value(i);
                    //添加
                    builder.add(key, value);
                }
                builder.add("source", "android");
                builder.add("token", "appVersion");
                FormBody newBody = builder.build();
                //创建新的请求
                Request request1 = request.newBuilder().post(newBody).build();
                //让request1去重新请求,proceed所有http请求的核心
                Response response = chain.proceed(request1);
                return response;
            }
            return null;
        }
    }

    //上传头像
    public void upLoadImage(String upload_url, File file) {
        //创建OKHttpClient
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建MultiPartBody
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        //设置参数
        MediaType mediaType = MediaType.parse("image/png");
        builder.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file));
        //添加其他参数
        builder.addFormDataPart("uid", "71");

        MultipartBody body = builder.build();
        //创建请求队列
        Request request = new Request.Builder().url(upload_url).post(body).build();
        Call newCall = okHttpClient.newCall(request);
        newCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure() returned: " + "shibai---" + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, "onResponse() returned: " + response.body().string());
            }
        });
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值