当前位置 : 主页 > 编程语言 > c++ >

OkHttp3封装

来源:互联网 收集:自由互联 发布时间:2021-07-03
OkHtt3封装 package com.app.test.apkupdateproject.net;import java.io.IOException;import java.net.SocketTimeoutException;import java.util.Map;import java.util.concurrent.TimeUnit;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Fo
OkHtt3封装
package com.app.test.apkupdateproject.net;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by ${liumengqiang} on 2017/9/12.
 */

public class OkhttpManager {
    private static OkhttpManager okhttpManager;
    private static OkHttpClient okHttpClient;
    private OkhttpManager(){
    }
    public static OkhttpManager getInstance(){
        if(okhttpManager == null){
            okhttpManager = new OkhttpManager();
            okHttpClient = new OkHttpClient().newBuilder().readTimeout(5000, TimeUnit.MILLISECONDS)
                        .writeTimeout(5000,TimeUnit.MILLISECONDS)
                        .connectTimeout(5000,TimeUnit.MILLISECONDS)
                        .build();
        }
        return okhttpManager;
    }
    public void doPost(Map
 
   map, String url, final OKHttpCallBack okHttpCallBack){
        RequestBody requestBody = getRequestBody(map);
        Request request = new Request.Builder().post(requestBody).url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if(e instanceof SocketTimeoutException){
                    okHttpCallBack.onFail(0, "time connect out!");
                }else{
                    okHttpCallBack.onFail(1,e.getMessage() == null ? "net error" : e.getMessage());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String string = response.body().string();
                    /**
                     * TODO 解析
                     */
                    okHttpCallBack.onSucess(string);
                }else{
                    okHttpCallBack.onFail(response.code(), parseCode(response.code()));
                }

            }
        });
    }

    public void doGet(String url, final OKHttpCallBack okHttpCallBack){
        Request request = new Request.Builder().get().url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if(e instanceof SocketTimeoutException){
                    okHttpCallBack.onFail(0,"time connect out!");
                }else{
                    okHttpCallBack.onFail(1,e.getMessage() == null ? "net error!" : e.getMessage());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String string = response.body().string();
                    /**
                     * TODO 解析
                     */
                    okHttpCallBack.onSucess(string);
                }else{
                    okHttpCallBack.onFail(response.code(), parseCode(response.code()));
                }
            }
        });
    }
    public interface OKHttpCallBack{
        void onSucess(String infoString);
        void onFail(int code, String error);
    }

    /**
     * 与服务器统一
     * @param code
     * @return
     */
    private String parseCode(int code) {
        if (code < 200 || code > 599) {
            return "未知异常";
        } else if (code > 399 && code < 600) {
            return "连接服务器异常";
        } else {
            return "服务器响应异常";
        }
    }
    /**
     *
     */
    private RequestBody getRequestBody(Map
  
    map){ /** * timestamp=2013-10-11 14:00:000&customeCode=bsj123456&method=category.list.query &bizSource=123456&sign=f60579d881fd9f0a698303329ae4a97b&requestBizData={具体业务请求数据,格式为json字符串,并且进行了url编码,详细见各接口说明} */ FormBody.Builder builder = new FormBody.Builder(); for(Map.Entry
   
     entry: map.entrySet()){ builder.add(entry.getKey(), entry.getValue()); } return builder.build(); } }
   
  
 
网友评论