日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 对okhttp的封装

發(fā)布時間:2024/4/15 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 对okhttp的封装 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

不廢話,需求:根據(jù)服務器API封裝網(wǎng)絡請求,怎么辦?

?

簡單封裝okhttp的get,post,put,delete請求:

PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));HttpUtils.okHttpClient = new OkHttpClient.Builder().cookieJar(cookieJar).build();

?

/*** 為HttpGet 的 url 方便的添加多個name value 參數(shù)。** @param url* @param params* @return*/public static String attachHttpGetParams(String url, LinkedHashMap<String, String> params) {Iterator<String> keys = params.keySet().iterator();Iterator<String> values = params.values().iterator();StringBuffer stringBuffer = new StringBuffer();stringBuffer.append("?");for (int i = 0; i < params.size(); i++) {String value = null;try {value = URLEncoder.encode(values.next(), "utf-8");} catch (Exception e) {e.printStackTrace();}stringBuffer.append(keys.next() + "=" + value);if (i != params.size() - 1) {stringBuffer.append("&");}}return url + stringBuffer.toString();}public static void HTTP_GET_IMPROVE(final String url, final HTTPInterface httpInterface, final int count) {Request request = builder.url(url).method("GET", null).build();//3.創(chuàng)建一個call對象,參數(shù)就是Request請求對象Call call = okHttpClient.newCall(request);//4.請求加入調度,重寫回調方法call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {httpInterface.onFailure(call, e);}@Overridepublic void onResponse(Call call, Response response) throws IOException {int code = response.code(); // Log.e("HTTP_GET_IMPROVE", " code=" + code + " url=" + url + " count=" + count);int myCount = count - 1;if (code != 200 && count > 0) {HTTP_GET_IMPROVE(url, httpInterface, myCount);} else {httpInterface.onResponse(call, response);}}});}public static void HTTP_POST_IMPROVE(final String url, RequestBody requestBody, final HTTPInterface httpInterface, final int count) {//1.創(chuàng)建OkHttpClient對象 // OkHttpClient okHttpClient = new OkHttpClient();//2.通過new FormBody()調用build方法,創(chuàng)建一個RequestBody,可以用add添加鍵值對//3.創(chuàng)建Request對象,設置URL地址,將RequestBody作為post方法的參數(shù)傳入 // Request request = new Request.Builder().url(url).post(requestBody).build();final Request request = builder.url(url).post(requestBody).build();//4.創(chuàng)建一個call對象,參數(shù)就是Request請求對象Call call = okHttpClient.newCall(request);//5.請求加入調度,重寫回調方法call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {httpInterface.onFailure(call, e);}@Overridepublic void onResponse(Call call, Response response) throws IOException {int code = response.code();int myCount = count - 1;if (code != 200 && count > 0) {HTTP_GET_IMPROVE(url, httpInterface, myCount);} else {// Log.e("TAG", "POST JSESSIONID= " + response.header("JSESSIONID"));httpInterface.onResponse(call, response);}}});}public static void HTTP_PATCH_IMPROVE(final String url, RequestBody requestBody, final HTTPInterface httpInterface, final int count) {//1.創(chuàng)建OkHttpClient對象 // OkHttpClient okHttpClient = new OkHttpClient();//2.創(chuàng)建Request對象,設置一個url地址(百度地址),設置請求方式。Request request = new Request.Builder().url(url).method("PATCH", requestBody).build();//3.創(chuàng)建一個call對象,參數(shù)就是Request請求對象Call call = okHttpClient.newCall(request);//4.請求加入調度,重寫回調方法call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {httpInterface.onFailure(call, e);}@Overridepublic void onResponse(Call call, Response response) throws IOException {int code = response.code();int myCount = count - 1;if (code != 200 && count > 0) {HTTP_GET_IMPROVE(url, httpInterface, myCount);} else {httpInterface.onResponse(call, response);}}});}public static void HTTP_PUT_IMPROVE(final String url, RequestBody requestBody, final HTTPInterface httpInterface, final int count) {//1.創(chuàng)建OkHttpClient對象 // OkHttpClient okHttpClient = new OkHttpClient();//2.創(chuàng)建Request對象,設置一個url地址(百度地址),設置請求方式。Request request = new Request.Builder().url(url).method("PUT", requestBody).build();//3.創(chuàng)建一個call對象,參數(shù)就是Request請求對象Call call = okHttpClient.newCall(request);//4.請求加入調度,重寫回調方法call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {httpInterface.onFailure(call, e);}@Overridepublic void onResponse(Call call, Response response) throws IOException {int code = response.code();int myCount = count - 1;if (code != 200 && count > 0) {HTTP_GET_IMPROVE(url, httpInterface, myCount);} else {httpInterface.onResponse(call, response);}}});}public static void HTTP_DELETE_IMPROVE(final String url, RequestBody requestBody, final HTTPInterface httpInterface, final int count) {//1.創(chuàng)建OkHttpClient對象 // OkHttpClient okHttpClient = new OkHttpClient();//2.通過new FormBody()調用build方法,創(chuàng)建一個RequestBody,可以用add添加鍵值對//3.創(chuàng)建Request對象,設置URL地址,將RequestBody作為post方法的參數(shù)傳入Request request = new Request.Builder().url(url).delete(requestBody).build();//4.創(chuàng)建一個call對象,參數(shù)就是Request請求對象Call call = okHttpClient.newCall(request);//5.請求加入調度,重寫回調方法call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {httpInterface.onFailure(call, e);}@Overridepublic void onResponse(Call call, Response response) throws IOException {int code = response.code();int myCount = count - 1;if (code != 200 && count > 0) {HTTP_GET_IMPROVE(url, httpInterface, myCount);} else {httpInterface.onResponse(call, response);}}});}/***/public static void HTTPUploadImage(final String url, final String imagePath, final HTTPInterface httpInterface, final int count) {Log.e("imagePath", "HTTPUploadImage " + imagePath);File file = new File(imagePath);RequestBody image = RequestBody.create(MediaType.parse("image/png"), file);RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("files", imagePath, image).build();Request request = new Request.Builder().url(url).post(requestBody).build();Call call = okHttpClient.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {}@Overridepublic void onResponse(Call call, Response response) throws IOException {int code = response.code();int myCount = count - 1;if (code != 200 && count > 0) {HTTPUploadImage(url, imagePath, httpInterface, myCount);} else {httpInterface.onResponse(call, response);}}});}

根據(jù)服務器的API封裝:

public static void outProduct(String token, String product_id, HttpUtils.HTTPInterface httpInterface) {RequestBody requestBody = new FormBody.Builder().add("token", token).add("product_id", product_id).build();String url = HttpUtils.OUT_PRODUCT;HttpUtils.HTTP_POST_IMPROVE(url, requestBody, httpInterface, HttpUtils.HTTP_TOTAL_COUNT);}


這樣的好處是網(wǎng)絡請求的API與具體的界面分離了,我之前寫代碼,每次寫完界面,再做界面的請求,導致很多界面有同樣的網(wǎng)絡請求,不停復制,修改代碼,浪費時間。封裝代碼也是一個程序員技術提升的體現(xiàn)。

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的Android 对okhttp的封装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。