andriod中對OkhttpClient請求的封裝及

為了不重復去寫代碼侠坎,對okhttpClient請求做封裝咒循,包含了如何跳過請求證書驗證杀餐,以及請求攔截器中對報文加密干发,響應攔截器中報文解密的實現(xiàn)如下:
1、modle的build.gradle添加依賴
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
implementation 'com.alibaba:fastjson:1.2.4'
2史翘、RequestService代碼如下:

import retrofit2.Call;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Streaming;
import retrofit2.http.Url;

/**
 * 作者:created by fanHongJiao
 */
public class RequestService {
    public interface commonService {
        @Headers({"Content-type:application/json;charset=UTF-8"})
        @POST("{reqPath}")
        Call<ResponseBody> createCommit(@Path(value = "reqPath", encoded = true) String path, @Body RequestBody route);

        @Headers({"Content-type:application/json;charset=UTF-8"})
        @POST("")
        Call<ResponseBody> createUilCommit(@Url String url, @Body RequestBody route);
    }

    public interface downloadFileService {
        @Streaming
        @GET
        Call<ResponseBody> downloadFileWithDynamicUrlSync(@Url String fileUrl);
    }

}

3枉长、RequestBodyFactory代碼如下

package okhttpclientDemo.utils;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * 作者:created by fanHongJiao
 */
public class RequestBodyFactory {
    public static Call<ResponseBody> requestNet(Object body,String reqPath) throws JSONException {
        RequestService.commonService service=NetManger.getInstance().CreateService(RequestService.commonService.class);
        JSONObject jsonObject=new JSONObject();
        JSONObject heard=new JSONObject();
        Date time= Calendar.getInstance().getTime();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        heard.put("time",format.format(time));
        jsonObject.put("heard",heard);
        RequestBody requestBody=RequestBody.create(MediaType.parse("Content-Type,application/json"),jsonObject.toString());
        Call<ResponseBody> commit=service.createCommit(reqPath,requestBody);
        return commit;
    }
}

4、NetManger代碼

package okhttpclientDemo.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
import okhttpclientDemo.utils.e2ee.RequestInterceptor;
import okhttpclientDemo.utils.e2ee.ResponseInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者:created by fanHongJiao
 */
public class NetManger {
    private static NetManger instance;
    Retrofit retrofit;

    public static NetManger getInstance() {
        if (instance == null) {
            instance = new NetManger();
        }
        return instance;
    }

    private NetManger() {

        //跳過http驗證
        X509TrustManager xtm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };

        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[]{xtm}, new SecureRandom());

        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }

        HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };

        OkHttpClient client = new OkHttpClient().newBuilder()
                .sslSocketFactory(sslContext.getSocketFactory())//跳過hhtp驗證
                .hostnameVerifier(DO_NOT_VERIFY)
                .addInterceptor(new RequestInterceptor())//攔截器
                .addInterceptor(new ResponseInterceptor())
                .connectTimeout(5, TimeUnit.SECONDS)//鏈接超時時間
                .readTimeout(120, TimeUnit.SECONDS)
                .callTimeout(120, TimeUnit.SECONDS)
                .build();

        //配置你的gson
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd hh:mm:ss")
                .create();
        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.BaseUrl)//url
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(client)
                .build();
    }

    public <T> T CreateService(Class tClass) {
        T service = (T) retrofit.create(tClass);
        return service;
    }

}

5琼讽、IResponse代碼如下:

package okhttpclientDemo.utils;

import org.json.JSONObject;

/**
 *
 * 作者:created by fanHongJiao
 */
public interface IResponse {
    void success(JSONObject object);
    void failed(String code,String message,Object obj);
}

6必峰、HttpRequestManager代碼如下:

package okhttpclientDemo.utils;

import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * 作者:created by fanHongJiao
 */
public class HttpRequestManager {
    public static final void postRequestObject(JSONObject body,String path,final IResponse callBack) throws JSONException {
        Call<ResponseBody>responseBodyCall=RequestBodyFactory.requestNet(body,path);
        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String string = response.body().string();
                    callBack.success(body);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                callBack.failed("-3", t.getMessage(), t);
                //連接失敗
            }
        });
    }
}

7、Constant代碼如下:

package okhttpclientDemo.utils;

/**
 *常量定義
 * 作者:created by fanHongJiao
 */
public class Constant {
    public static  final  String BaseUrl="https://220.241.39.45/cibs/sit/";
    public static  final  String requestUrl="custpc/generateGetSessionKey.do";
}

8钻蹬、E2eeUtils用于模擬加解密吼蚁,具體如何加密根據(jù)需求

package okhttpclientDemo.utils.e2ee;

/**
 * 模仿加密,如何加密根據(jù)需求添加代碼
 * 作者:created by fanHongJiao
 */
public class E2eeUtils {
    public E2eeUtils() {
    }

    /**
     * 加密操作
     * @param bytes
     * @return
     */
    public String encrypt(byte[] bytes) {
        String enStr = null;
        return enStr;
    }

    /**
     * 解密
     * @param bytes
     * @return
     */
    public String decrypt(byte[] bytes){
        String decStr=null;
        return decStr;
    }
}

9问欠、RequestInterceptor請求攔截器肝匆,在請求發(fā)出之前執(zhí)行,用于放加密顺献,統(tǒng)一的請求頭參數(shù)代碼如下:

package okhttpclientDemo.utils.e2ee;
import java.io.IOException;
import java.nio.charset.Charset;
import okhttp3.MediaType;
import okhttpclientDemo.utils.e2ee.E2eeUtils;
import okio.Buffer;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
 * 請求攔截器,可對報文加密旗国, 或者添加公共參數(shù)等
 * 作者:created by fanHongJiao
 */
public class RequestInterceptor implements Interceptor {
    private static boolean isE2ee = false;//是否加密,true表示需要加密
    private static final String CHARSET = "UTF-8";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        RequestBody requestBody = request.body();
        String requestUrl = request.url().toString();
        //判斷是否需要加密
        if (isE2ee) {
            Buffer buffer = new Buffer();
            //做加密注整,以簡單的md5加密為例
            E2eeUtils e2eeUtils = new E2eeUtils();
            requestBody.writeTo(buffer);
            Charset charset;
            MediaType contentType = requestBody.contentType();
            charset = contentType.charset(Charset.forName("UTF-8"));
            String requestString = buffer.readString(charset);//讀取請求體
            String encrytParams = null;
            encrytParams = e2eeUtils.encrypt(requestString.getBytes(CHARSET));//加密

            RequestBody body = chain.request().body();
            MediaType contentType1 = body.contentType();
            if(encrytParams!=null){
            requestBody = RequestBody.create(contentType1, encrytParams.trim());}
            request = chain.request()
                    .newBuilder()
                    .header("Accept-Language", "en")//okhttp請求頭提交語言
                    .post(requestBody)
                    .build();
        }
        return chain.proceed(request);
    }
}

10能曾、ResponseInterceptor請求數(shù)據(jù)返回時執(zhí)行,可處理報文解密肿轨,代碼如下:

package okhttpclientDemo.utils.e2ee;

import java.io.IOException;
import java.nio.charset.Charset;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttpclientDemo.utils.e2ee.E2eeUtils;
import okio.Buffer;
import okio.BufferedSource;
/**
 * 請求響應攔截器借浊,處理返回的加密報文得解密
 * 作者:created by fanHongJiao
 */
public class ResponseInterceptor implements Interceptor {
    private static boolean isE2ee = false;//是否解密,true表示需要解密
    private static final String CHARSET = "UTF-8";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request=chain.request();
        Response response=chain.proceed(request);
        ResponseBody responseBody=response.body();
        if(responseBody!=null){
            //獲取響應body
            BufferedSource source=responseBody.source();
            source.request(Long.MAX_VALUE);
            Buffer buffer=source.buffer();
            Charset charset=Charset.forName("UTF-8");
            MediaType contentType=responseBody.contentType();
            charset=contentType.charset(charset);
            String bodyString=buffer.clone().readString(charset);
            String responseData=null;
            if(isE2ee){
                E2eeUtils e2eeUtils=new E2eeUtils();
                responseData=e2eeUtils.decrypt(bodyString.getBytes(CHARSET));//解密
                if(responseData!=null) {
                    ResponseBody newResponseBody = ResponseBody.create(contentType, responseData);

                response=response
                        .newBuilder()
                        .body(newResponseBody)
                        .build();
                return response;
                }

            }
        }
        return response;
    }
}

11萝招、OkHttpClientActivity蚂斤,測試請求頁面代碼如下:

package okhttpclientDemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.example.viewpaerdemo.R;
import org.json.JSONException;
import org.json.JSONObject;
import okhttpclientDemo.utils.Constant;
import okhttpclientDemo.utils.HttpRequestManager;
import okhttpclientDemo.utils.IResponse;
public class OkHttpClientActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ok_http_client);
        JSONObject body=new JSONObject();
        try {
            body.put("heard","heard");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            HttpRequestManager.postRequestObject(body, Constant.requestUrl, new IResponse() {
                @Override
                public void success(JSONObject object) {
                    Log.e("success:",object.toString());
                }

                @Override
                public void failed(String code, String message, Object obj) {
                    Log.e("failed:",obj.toString());
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

完結。

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末槐沼,一起剝皮案震驚了整個濱河市曙蒸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌岗钩,老刑警劉巖纽窟,帶你破解...
    沈念sama閱讀 211,884評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異兼吓,居然都是意外死亡臂港,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來审孽,“玉大人县袱,你說我怎么就攤上這事∮恿Γ” “怎么了式散?”我有些...
    開封第一講書人閱讀 157,435評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長打颤。 經(jīng)常有香客問我暴拄,道長,這世上最難降的妖魔是什么编饺? 我笑而不...
    開封第一講書人閱讀 56,509評論 1 284
  • 正文 為了忘掉前任乖篷,我火速辦了婚禮,結果婚禮上透且,老公的妹妹穿的比我還像新娘那伐。我一直安慰自己,他們只是感情好石蔗,可當我...
    茶點故事閱讀 65,611評論 6 386
  • 文/花漫 我一把揭開白布罕邀。 她就那樣靜靜地躺著,像睡著了一般养距。 火紅的嫁衣襯著肌膚如雪诉探。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,837評論 1 290
  • 那天棍厌,我揣著相機與錄音肾胯,去河邊找鬼。 笑死耘纱,一個胖子當著我的面吹牛敬肚,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播束析,決...
    沈念sama閱讀 38,987評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼艳馒,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了员寇?” 一聲冷哼從身側響起弄慰,我...
    開封第一講書人閱讀 37,730評論 0 267
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蝶锋,沒想到半個月后陆爽,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,194評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡扳缕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,525評論 2 327
  • 正文 我和宋清朗相戀三年慌闭,在試婚紗的時候發(fā)現(xiàn)自己被綠了别威。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,664評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡驴剔,死狀恐怖省古,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情仔拟,我是刑警寧澤衫樊,帶...
    沈念sama閱讀 34,334評論 4 330
  • 正文 年R本政府宣布飒赃,位于F島的核電站利花,受9級特大地震影響,放射性物質發(fā)生泄漏载佳。R本人自食惡果不足惜炒事,卻給世界環(huán)境...
    茶點故事閱讀 39,944評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蔫慧。 院中可真熱鬧挠乳,春花似錦、人聲如沸姑躲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,764評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽黍析。三九已至卖怜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間阐枣,已是汗流浹背马靠。 一陣腳步聲響...
    開封第一講書人閱讀 31,997評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蔼两,地道東北人甩鳄。 一個月前我還...
    沈念sama閱讀 46,389評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像额划,于是被迫代替她去往敵國和親妙啃。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,554評論 2 349

推薦閱讀更多精彩內(nèi)容