Android中OkHttp的使用

網(wǎng)上有很多OkHttp使用的教程,而我今天要給大家分享的是我自己對(duì)OkHttp的使用并且做了簡(jiǎn)單的封裝,希望能給你們帶來幫助,話不多說了下面直接上干貨了(我使用的是Android Studio)。

官方的文檔

OkHttp官網(wǎng)地址:http://square.github.io/okhttp/
OkHttp GitHub地址:https://github.com/square/okhttp

OKHttp的使用

Android Studio用戶在 Gradle中添加: 
  compile'com.squareup.okhttp3:okhttp:3.8.1'
  compile 'com.squareup.okio:okio:1.13.0'
Eclipse的用戶,添加依賴包即可,Jar包下載地址: okhttp3_Jar okio_Jar

干貨來了(直接上代碼)

1.請(qǐng)求地址配置

/**
 * 網(wǎng)絡(luò)訪問地址定義
 * @author zcq
 * Created by 2016/4/25.
 */
public class ApiConfig {
    public final static String API_BSAE = "http://192.168.1.118/help/";
}

2.接口錯(cuò)誤定義

/**
 * 接口錯(cuò)誤定義
 * @author zcq
 * Created by 2017/4/25.
 */
public enum  ErrorCode {

    /**
     * 無(wú)錯(cuò)誤
     */
    None,

    /**
     * 網(wǎng)絡(luò)錯(cuò)誤
     */
    NetworkError,

    /**
     * 服務(wù)器錯(cuò)誤
     */
    ServerError,

    /**
     * 數(shù)據(jù)錯(cuò)誤
     */
    DataError
}

3.數(shù)據(jù)接口監(jiān)聽器

/**
 * 數(shù)據(jù)偵聽器接口定義,用于向接口傳遞單條數(shù)據(jù)
 * @author zcq
 * Created by 2016/4/25.
 */
public interface OnDataListener<T> extends OnErrorListener{
    /**
     * 向接口傳遞單條數(shù)據(jù)
     * @param isSuccess 接口調(diào)用是否成功
     * @param data 待傳遞的單條數(shù)據(jù)
     * @param errorCode 接口調(diào)用失敗時(shí)錯(cuò)誤碼
     * @param errorReason 接口調(diào)用失敗時(shí)的原因
     */
    void onData(boolean isSuccess, T data, int errorCode, String errorReason);
}
/**
 * 數(shù)據(jù)偵聽器接口定義,用于向接口傳遞單條數(shù)據(jù)
 * @author zcq
 * Created by 2016/4/25.
 */
public interface  OnListListener<T> extends OnErrorListener{
    /**
     * 向接口傳遞多條數(shù)據(jù)
     * @param isSuccess 接口調(diào)用是否成功
     * @param data 待傳遞的多條數(shù)據(jù)
     * @param errorCode 接口調(diào)用失敗時(shí)錯(cuò)誤碼
     * @param errorReason 接口調(diào)用失敗時(shí)的原因
     */
    void onList(boolean isSuccess, List<T> data, int errorCode, String errorReason);
}
/**
 * @author zcq
 * Created by 2016/4/25.
 */
public interface OnImageListener {
    void onImage(int position, String url, Bitmap bitmap);
}
/**
 * 錯(cuò)誤偵聽器接口定義,用于向接口傳遞錯(cuò)誤信息
 * @author zcq
 * Created by 2016/4/25.
 */
public interface OnErrorListener {

    /**
     * 向接口傳遞錯(cuò)誤信息
     * @param code 待傳遞的錯(cuò)誤信息
     */
    void onError(ErrorCode code);
}

4.與服務(wù)器通訊接口類定義

/**
 * 與服務(wù)器通訊接口類定義
 * @author zcq
 * Created by 2016/4/25.
 */
public class HttpHelper {
    private final static boolean DEBUG = true;
    private final static String TAG = "help/HttpHelper";
    protected final static int MESSAGE_DATA = 0;
    protected final static int MESSAGE_ERROR = 1;

    /**
     * 接收到網(wǎng)絡(luò)數(shù)據(jù)
     * @param event 事件的標(biāo)識(shí)
     * @param response 所接收到的數(shù)據(jù)
     */
    protected void onData(int event, String response){
    }

    /**
     * 接收到網(wǎng)絡(luò)錯(cuò)誤
     * @param event 事件的標(biāo)識(shí)
     * @param code 所接收到的錯(cuò)誤碼
     */
    protected void onError(int event, ErrorCode code){
    }

    static class UIHandler extends Handler {
        private final WeakReference<HttpHelper> ref;

        UIHandler(HttpHelper act) {
            this.ref = new WeakReference<>(act);
        }

        @Override
        public void handleMessage(Message msg) {
            HttpHelper act = ref.get();
            if (msg.what == MESSAGE_DATA) {
                act.onData(msg.arg1, msg.obj != null ? msg.obj.toString() : null);
            } else if (msg.what == MESSAGE_ERROR) {
                act.onError(msg.arg1, (ErrorCode) msg.obj);
            } else {
                super.handleMessage(msg);
            }
        }
    }

    private final UIHandler mHandler = new UIHandler(this);

    /**
     * 開始異步訪問網(wǎng)絡(luò)接口
     * @param event 事件編號(hào)
     * @param url 接口地址
     * @param params 待提交的參數(shù)
     */
    protected synchronized void beginRequest(final int event, final String url, final Map<String, String> params){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                FormBody.Builder formBodayBuilder = new FormBody.Builder();
                //參數(shù)處理
                if (params != null) {
                    for (String param : params.keySet()) {
                        String value = params.get(param);
                        if (value != null) {
                            formBodayBuilder.add(param, value);
                        }
                    }
                }

                //設(shè)置發(fā)出的 request
                Request post = new Request.Builder()
                        .url(url) //請(qǐng)求的地址
                        .post(formBodayBuilder.build()) //請(qǐng)求的參數(shù)
                        .tag(event) //請(qǐng)求標(biāo)識(shí)
                        .build();
                try {
                    //獲取 response
                    Response response = client.newCall(post).execute();
                    if (response.isSuccessful()) {
                        String res = response.body().string();
//                        Log.i("----API請(qǐng)求返回值----","----***************---res:"+res);
                        if (res != null) {
                            mHandler.obtainMessage(MESSAGE_DATA, event, 0, res).sendToTarget();
                        } else {
                            mHandler.obtainMessage(MESSAGE_ERROR, event, 0, ErrorCode.ServerError).sendToTarget();
                        }
                    } else {
                        if (DEBUG) {
                            String res = response.body().toString();
                            if (res != null) {
                                Log.d(TAG, res);
                            }
                        }
                        mHandler.obtainMessage(MESSAGE_ERROR, event, 0, ErrorCode.ServerError).sendToTarget();
                    }
                } catch (IOException e) {
                    mHandler.obtainMessage(MESSAGE_ERROR, event, 0, ErrorCode.NetworkError).sendToTarget();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 開始異步上傳文件
     * @param event 事件編號(hào)
     * @param url 接口地址
     * @param params 待提交的參數(shù)
     *filesam files 待上傳的文件
     */
    protected synchronized void beginUpload(final int event, final String url,
                                            final Map<String, String> params,
                                            final Map<String, String> files) {
        new Thread(new Runnable() {
            @Override
            public synchronized void run() {
                OkHttpClient client = new OkHttpClient();
                //以表單方式上傳圖片文件
                MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
                //非文件參數(shù)處理
                if (params != null) {
                    for (String param : params.keySet()) {
                        String value = params.get(param);
                        if (value != null) {
                            builder.addFormDataPart(param, value);
                        }
                    }
                }
                //文件參數(shù)處理 TODO: MediaType 可提出作為參數(shù)
                MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
                int i = 0;
                if(files != null){
                    for (String key : files.keySet()){
                        File file = new File(files.get(key));
                        if(file != null){
                            builder.addFormDataPart("file"+i, file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
                        }
                        i++;
                    }
                }
                MultipartBody requestBody = builder.build();

                //請(qǐng)求
                Request post = new Request.Builder()
                        .url(url) //請(qǐng)求的地址
                        .post(requestBody) //請(qǐng)求的參數(shù)
                        .tag(event) //請(qǐng)求標(biāo)識(shí)
                        .build();

                try {
                    //獲取 response
                    Response response = client.newCall(post).execute();
                    if (response.isSuccessful()) {
                        String res = response.body().string();
                        if (res != null) {
                            mHandler.obtainMessage(MESSAGE_DATA, event, 0, res).sendToTarget();
                        } else {
                            mHandler.obtainMessage(MESSAGE_ERROR, event, 0, ErrorCode.ServerError).sendToTarget();
                        }
                    } else {
                        if (DEBUG) {
                            String res = response.body().toString();
                            if (res != null) {
                                Log.d(TAG, res);
                            }
                        }
                        mHandler.obtainMessage(MESSAGE_ERROR, event, 0, ErrorCode.ServerError).sendToTarget();
                    }
                } catch (IOException e) {
                    mHandler.obtainMessage(MESSAGE_ERROR, event, 0, ErrorCode.NetworkError).sendToTarget();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 開始異步訪問網(wǎng)絡(luò)接口
     * @param event 事件編號(hào)
     * @param url 接口地址
     */
    protected synchronized void beginRequest(int event, String url) {
        beginRequest(event, url, null);
    }
}

5.返回的 JSON 數(shù)據(jù)進(jìn)行解析的幫助類

/**
 * 對(duì) HTTP 返回的 JSON 數(shù)據(jù)進(jìn)行解析的幫助類
 * @author zcq
 * Created by 2016/4/25.
 */
public class JsonHttpHelper extends HttpHelper{

    private final static boolean DEBUG = true;
    private final static String TAG = "help/HttpHelper";

    /**
     * 接收到網(wǎng)絡(luò)數(shù)據(jù)
     *
     * @param event    事件的標(biāo)識(shí)
     * @param response 所接收到的數(shù)據(jù)
     */
    @Override
    protected void onData(int event, String response) {
        try {
            // 開始解析數(shù)據(jù)
            JSONObject jsonObject = new JSONObject(response);
            JsonResult jsonResult = new JsonResult();

            jsonResult.isSuccess = jsonObject.optBoolean("IsSuccess");

            if (jsonResult.isSuccess) {
                jsonResult.returnValue = jsonObject.optString("ReturnValue");
            } else {
                jsonResult.errorCode = jsonObject.optInt("ErrorCode");
                jsonResult.errorReason = jsonObject.optString("ErrorMessage");
            }
            onResult(event, jsonResult);
        } catch (JSONException e) {
            onError(event, ErrorCode.DataError);
        }
    }

    /**
     * 接口調(diào)用完成時(shí)解析后的數(shù)據(jù)
     *
     * @param event  事件的標(biāo)識(shí)
     * @param result 解析后的數(shù)據(jù)
     */
    protected void onResult(int event, JsonResult result) {
    }

    /**
     * 將解析后的數(shù)據(jù)交給指定偵聽器進(jìn)行處理
     *
     * @param result   待處理的數(shù)據(jù)
     * @param cls      返回值對(duì)應(yīng)的類
     * @param <T>      返回值對(duì)應(yīng)的數(shù)據(jù)類型
     * @param listener 指定的偵聽器
     */
    @SuppressWarnings("unchecked")
    protected <T> void handleData(@NonNull JsonResult result, @NonNull Class<T> cls, @Nullable OnErrorListener listener) {
        // 檢查偵聽器有無(wú)注冊(cè)
        if (listener == null) {
            if (DEBUG) {
                Log.d(TAG, "the listener not used.");
            }
            return;
        }

        // 接口返回失敗時(shí)不解析返回值
        if (!result.isSuccess) {
            if (listener instanceof OnDataListener) {
                ((OnDataListener<T>) listener).onData(false, null, result.errorCode, result.errorReason);
            } else if (listener instanceof OnListListener) {
                ((OnListListener<T>) listener).onList(false, null, result.errorCode, result.errorReason);
            }
            return;
        }

        // 將返回值字符串解析成對(duì)象
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            if (listener instanceof OnDataListener) {
                // 返回的數(shù)據(jù)為單個(gè)對(duì)象
                T returnValue;

                if (cls.getSimpleName().equals("String")) {
                    returnValue = (T) result.returnValue;
                } else {
                    returnValue = objectMapper.readValue(result.returnValue, cls);
                }
                ((OnDataListener<T>) listener).onData(true, returnValue, 0, null);

            } else if (listener instanceof OnListListener) {
                // 返回的數(shù)據(jù)為列表數(shù)據(jù)
                List<T> returnValue = objectMapper.readValue(
                        result.returnValue, TypeFactory.defaultInstance()
                                .constructCollectionType(List.class, cls));

                ((OnListListener<T>) listener).onList(true, returnValue, 0, null);
            }
        } catch (IOException e) {
            listener.onError(ErrorCode.DataError);
        }
    }
}

6.請(qǐng)求返回值JSON格式定義

/**
 * 網(wǎng)絡(luò)請(qǐng)求返回值 JSON 格式定義
 * @author zcq
 * Created by 2016/4/25.
 */
public class JsonResult {

    /**
     * 判斷接口是否調(diào)用成功
     */
    public boolean isSuccess;

    /**
     * 接口返回值
     */
    public String returnValue;

    /**
     * 接口返回錯(cuò)誤時(shí)的Code
     */
    public int errorCode;

    /**
     * 接口返回的錯(cuò)誤信息
     */
    public String errorReason;
}

以上就是我使用OkHttp自定義的封裝請(qǐng)求相關(guān)的所有代碼了,下面則是使用的測(cè)試樣例

  • TestApi定義
/**
 * @author zcq
 * Created by 2016/4/26.
 */
public class TestApi extends JsonHttpHelper{

    //事件定義
    private final  static int EVENT_HOST_API_BASE = 1000;

    private final static int EVENT_GET_PERSONAGE_LIST = EVENT_HOST_API_BASE + 1;

    //訪問地址的定義
    private final static String HOST_SITE = ApiConfig.API_BSAE;

    //接口路徑
    private final static String URL_GET_PERSONAGE_LIST = HOST_SITE + "mobile/star/getDataLs"; //獲取數(shù)據(jù)列表接口

    /**
     * 接口調(diào)用完成時(shí)解析后的數(shù)據(jù)
     * @param event 事件的標(biāo)識(shí)
     * @param result 解析后的數(shù)據(jù)
     */
    @Override
    protected void onResult(int event, JsonResult result) {
        switch (event) {
            case EVENT_GET_PERSONAGE_LIST:
                handleData(result, Personage.class, mOnGetPersonageLsListener);
                break;
        }
    }

    /**
     * 接收到網(wǎng)絡(luò)錯(cuò)誤
     * @param event 事件的標(biāo)識(shí)
     * @param code 所接收到的錯(cuò)誤碼
     */
    @Override
    protected void onError(int event, ErrorCode code) {
        switch (event) {
            case EVENT_GET_PERSONAGE_LIST:
                if(mOnGetPersonageLsListener != null){
                    mOnGetPersonageLsListener.onError(code);
                }
                break;
        }
    }

    /*********************** 獲取List<Entity> **********************/

    //獲取數(shù)據(jù)列表接口
    private OnListListener<Personage> mOnGetPersonageLsListener;

    /**
     * 獲取數(shù)據(jù)列表接口
     * @param listener 待設(shè)置的偵聽器
     */
    public void setOnGetPersonageLsListener(OnListListener<Personage> listener){
        this.mOnGetPersonageLsListener = listener;
    }

    /**
     * 獲取數(shù)據(jù)列表接口
     * @param pageNum 當(dāng)前頁(yè)
     * @param pageSize 分頁(yè)數(shù)
     */
    public void getPersonageLs(int pageNum, int pageSize){
        Map<String, String> params = new HashMap<String, String>();
        params.put("pageNum", String.valueOf(pageNum));
        params.put("pageSize", String.valueOf(pageSize));
        params.put("channel_num","android");
        beginRequest(EVENT_GET_PERSONAGE_LIST, URL_GET_PERSONAGE_LIST, params);
    }

    /*********************** 獲取Entity String  Boolean**********************/
    //使用 OnDataListener<Entity>   OnDataListener<String>  OnDataListener<Boolean>
    //配置  handleData(result, Entity.class, mOnListener);
    //配置  handleData(result, Boolean.class, mOnListener);
    //配置  handleData(result, String.class, mOnListener);
}
  • 測(cè)試Activity
public class TestAct extends Activity{

    private TestApi mApi;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_ui_test_api);

        mApi = new TestApi();

        text = (TextView)findViewById(R.id.text);

        //Button按鈕
        findViewById(R.id.list_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mApi.getPersonageLs(0, 2);
            }
        });


        //設(shè)置偵聽器
        mApi.setOnGetPersonageLsListener(new OnListListener<Personage>() {
            @Override
            public void onList(boolean isSuccess, List<Personage> data, int errorCode, String errorReason) {
                if(isSuccess){
                    text.setText(data.get(0).desc);
                    Toast.makeText(TestAct.this, "List_成功!", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(TestAct.this, "List_失敗!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(ErrorCode code) {
                if(code.equals(ErrorCode.DataError)){
                    Toast.makeText(TestAct.this, "數(shù)據(jù)錯(cuò)誤!", Toast.LENGTH_SHORT).show();
                }else if(code.equals(ErrorCode.NetworkError)){
                    Toast.makeText(TestAct.this, "網(wǎng)絡(luò)錯(cuò)誤!", Toast.LENGTH_SHORT).show();
                }else if(code.equals(ErrorCode.ServerError)){
                    Toast.makeText(TestAct.this, "服務(wù)器錯(cuò)誤!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
  • 數(shù)據(jù)實(shí)體類 bean
/**
 * 實(shí)體類
 * @author zcq
 * Created by 2016/4/26.
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class Personage implements Serializable {

    @JsonProperty("column_id")
    public String id;

    @JsonProperty("column_name")
    public String name;

    @JsonProperty("column_context")
    public String context;

    @JsonProperty("column_desc")
    public String desc;

    @JsonProperty("column_icon")
    public String imgUrl;

    @JsonProperty("create_time")
    public String create_time;
}

最后附上API接口模板_樣例說明

Paste_Image.png
  • 返回值樣例(標(biāo)準(zhǔn)Json格式)
//返回值為Boolean樣例:
{
    "IsSuccess":true,
    "ReturnValue":true,
    "ErrorMessage":"",
    "ErrorCode":0
}

//返回值為String樣例:
{
    "IsSuccess":true,
    "ReturnValue":  "http://139.196.192.92/images/info/20160303/20160303110822_head_846141.jpg",
    "ErrorMessage":"",
    "ErrorCode":0
}

//返回值為L(zhǎng)ist<Entity>樣例:
{
    "IsSuccess":true,
    "ReturnValue":"[{
         "commentId":239,
         "infoId":6375,
         "authorId":40,
         "author":"青牙",
         "head_img_url":"http://139.196.192.92/images/info/20160303/20160303110822_head_846141.jpg",
         "comment_content":"感覺可憐可憐",
         "time":"2017-04-26"
        },
        {
         "commentId":238,
         "infoId":6375,
         "authorId":40,
         "author":"青牙",
         "head_img_url":"http://139.196.192.92/images/info/20160303/20160303110822_head_846141.jpg",
         "comment_content":"過來看看",
         "time":"2017-04-26"}]"
     "ErrorMessage":"",
     "ErrorCode":0
}

//返回值為Entity樣例:
{
    "IsSuccess":true,
    "ReturnValue":"{
         "infoId":6375,
         "title":"評(píng)《錦繡未央》抄襲:放任大\"IP\"抄襲只會(huì)劣幣驅(qū)逐良幣",
         "comment_count":4,
         "time":"2017-04-26",
         "is_like":"0",
         "token":"827885c0-db46-4234-a22b-af7aae0f31d9",
         "shareUrl":"http://139.196.192.92/cbtt/admin/info/viewDetailOnPhone?infoId=6375",
    }",
    "ErrorMessage":"",
    "ErrorCode":0
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容