以OKHttp為基礎封裝網絡請求工具類
來源:程序員人生 發布時間:2016-09-26 08:54:49 閱讀次數:2899次
特點:
1)支持SPDY協議,同享1個socket來處理同1個服務器的所有要求。
2)無縫支持GZIP,來減少數據流量。
3)緩存相應數據來減少重復的網絡要求。
網絡協議:
SPDY(讀作“SPeeDY”)是Google開發的基于TCP的利用層協議,用以最小化網絡延遲,提升網絡速度,優化用戶的網絡使用體驗。SPDY其實不是1種用于替換HTTP的協議,而是對HTTP協議的增強。新協議的功能包括數據流的多路復用、要求優先級和HTTP報頭緊縮。(利用層Http、網絡層、傳輸層協議)
<Volley框架實使用了spdy協議>
經常使用類:
OKHttpClient(客戶端對象)、
Request(訪問要求)、 RequestBody(要求實體對象)
Response(響應類)、 ResponseBody(響應結果類)
FormEncodingBuilder(表單構造器)、
MediaType(數據類型)
response.isSuccessful() response.body().string()
client.newCall(request)
Activity中的代碼:
package com.crs.demo.ui.okhttp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.crs.demo.R;
import com.crs.demo.base.BaseActivity;
import com.crs.demo.constant.UrlConstant;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
/**
* Created on 2016/9/19.
* Author:crs
* Description:OKHttp網絡要求框架的使用
*/
public class OKHttpActivity extends BaseActivity implements View.OnClickListener {
private Button btn_ok_http_get;
private Button btn_ok_http_post;
private TextView tv_ok_http_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_okhttp);
initViews();
initListener();
}
private void initViews() {
btn_ok_http_get = findView(R.id.btn_ok_http_get);
btn_ok_http_post = findView(R.id.btn_ok_http_post);
tv_ok_http_content = findView(R.id.tv_ok_http_content);
}
private void initListener() {
btn_ok_http_get.setOnClickListener(this);
btn_ok_http_post.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_ok_http_get: {
clickGet();
}
break;
case R.id.btn_ok_http_post: {
clickPost();
}
break;
}
}
private void clickPost() {
HttpUtils httpUtils = new HttpUtils();
HashMap<String, String> params = new HashMap<>();
params.put("orderNo", "TH01587458");
httpUtils.post(UrlConstant.ORDER_STATUS_POST, params, new HttpUtils.BaseCallBack() {
@Override
public void onSuccess(Response response, String json) {
JSONObject jsonObject;
try {
jsonObject = new JSONObject(json);
String status = jsonObject.getString("Status");
tv_ok_http_content.setText(status);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFail(Request request, IOException e) {
}
@Override
public void onError(Response response, int code) {
}
});
}
private void clickGet() {
HttpUtils httpUtils = new HttpUtils();
httpUtils.get(UrlConstant.ORDER_STATUS, new HttpUtils.BaseCallBack() {
@Override
public void onSuccess(Response response, String json) {
//使用json包解析 注意注解的使用
Gson gson = new Gson();
{
Entity entity = gson.fromJson(json, Entity.class);
AfterSaleType afterSaleType = entity.getAfterSaleType();
String shopService = afterSaleType.getShopService();
tv_ok_http_content.setText(shopService);
}
//只能使用內部類的情勢去封裝對象,這樣就不會報錯
{
//ResponseEntity responseEntity = gson.fromJson(json, ResponseEntity.class);
//ResponseEntity.AfterSaleType afterSaleType = responseEntity.getAfterSaleType();
//String shopService = afterSaleType.getShopServiceTousu();
//tv_ok_http_content.setText(shopService);
}
}
@Override
public void onFail(Request request, IOException e) {
}
@Override
public void onError(Response response, int code) {
}
});
}
}
網絡要求工具類HttpUtils中的代碼:
注意事項:
1)接口回調(回調結果到Activity中)。
2)子線程與主線程通訊。
3)OKHttp的兩個使用步驟。
package com.crs.demo.ui.okhttp;
import android.os.Handler;
import android.os.Looper;
import com.crs.demo.constant.IntConstant;
import com.crs.demo.utils.OKHttpUtils;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created on 2016/9/19.
* Author:crs
* Description:網絡要求工具類的封裝
*/
public class HttpUtils {
private OkHttpClient client;
private Handler mHandler;
public HttpUtils() {
client = new OkHttpClient();
//設置連接超時時間,在網絡正常的時候有效
client.setConnectTimeout(IntConstant.REQUEST_TIME_OUT, TimeUnit.SECONDS);
//設置讀取數據的超時時間
client.setReadTimeout(IntConstant.REQUEST_TIME_OUT, TimeUnit.SECONDS);
//設置寫入數據的超時時間
client.setWriteTimeout(IntConstant.REQUEST_TIME_OUT, TimeUnit.SECONDS);
//Looper.getMainLooper() 獲得主線程的消息隊列
mHandler = new Handler(Looper.getMainLooper());
}
public void get(String url, BaseCallBack baseCallBack) {
Request request = buildRequest(url, null, HttpMethodType.GET);
sendRequest(request, baseCallBack);
}
public void post(String url, HashMap<String, String> params, BaseCallBack baseCallBack) {
Request request = buildRequest(url, params, HttpMethodType.POST);
sendRequest(request, baseCallBack);
}
/**
* 1)獲得Request對象
*
* @param url
* @param params
* @param httpMethodType 要求方式不同,Request對象中的內容不1樣
* @return Request 必須要返回Request對象, 由于發送要求的時候要用到此參數
*/
private Request buildRequest(String url, HashMap<String, String> params, HttpMethodType httpMethodType) {
//獲得輔助類對象
Request.Builder builder = new Request.Builder();
builder.url(url);
//如果是get要求
if (httpMethodType == HttpMethodType.GET) {
builder.get();
} else {
RequestBody body = buildFormData(params);
builder.post(body);
}
//返回要求對象
return builder.build();
}
/**
* 2)發送網絡要求
*
* @param request
* @param baseCallBack
*/
private void sendRequest(Request request, final BaseCallBack baseCallBack) {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
callBackFail(baseCallBack,request, e);
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String json = response.body().string();
//此時要求結果在子線程里面,如何把結果回調到主線程里?
callBackSuccess(baseCallBack,response, json);
} else {
callBackError(baseCallBack,response, response.code());
}
}
});
}
/**
* 主要用于構建要求參數
*
* @param param
* @return ResponseBody
*/
private RequestBody buildFormData(HashMap<String, String> param) {
FormEncodingBuilder builder = new FormEncodingBuilder();
//遍歷HashMap集合
if (param != null && !param.isEmpty()) {
Set<Map.Entry<String, String>> entries = param.entrySet();
for (Map.Entry<String, String> entity : entries) {
String key = entity.getKey();
String value = entity.getValue();
builder.add(key, value);
}
}
return builder.build();
}
//要求類型定義
private enum HttpMethodType {
GET,
POST
}
//定義回調接口
public interface BaseCallBack {
void onSuccess(Response response, String json);
void onFail(Request request, IOException e);
void onError(Response response, int code);
}
//主要用于子線程和主線程進行通訊
private void callBackSuccess(final BaseCallBack baseCallBack, final Response response, final String json){
mHandler.post(new Runnable() {
@Override
public void run() {
baseCallBack.onSuccess(response,json);
}
});
}
private void callBackError(final BaseCallBack baseCallBack, final Response response, final int code){
mHandler.post(new Runnable() {
@Override
public void run() {
baseCallBack.onError(response,code);
}
});
}
private void callBackFail(final BaseCallBack baseCallBack, final Request request, final IOException e){
mHandler.post(new Runnable() {
@Override
public void run() {
//相當于此run方法是在主線程履行的,可以進行更新UI的操作
baseCallBack.onFail(request,e);
}
});
}
}
實體模型的封裝
注意事項:
1)使用注解@SerializedName("Code") 修改字段名
2)不要使用內部類嵌套的情勢封裝實體模型
Entity模型
package com.crs.demo.ui.okhttp;
import com.google.gson.annotations.SerializedName;
/**
* Created on 2016/9/19.
* Author:crs
* Description:要求結果實體模型
*/
public class Entity {
@SerializedName("Code")
private String code;
@SerializedName("AfterSaleType")
private AfterSaleType afterSaleType;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public AfterSaleType getAfterSaleType() {
return afterSaleType;
}
public void setAfterSaleType(AfterSaleType afterSaleType) {
this.afterSaleType = afterSaleType;
}
}
AfterSaleType模型:
package com.crs.demo.ui.okhttp;
import com.google.gson.annotations.SerializedName;
/**
* Created on 2016/9/19.
* Author:crs
* Description:AfterSaleType實體模型
*/
public class AfterSaleType {
@SerializedName("UnReceive")
private String unReceive;
@SerializedName("returnGoods")
private String ReturnGoods;
@SerializedName("ShopServiceTousu")
private String ShopService;
@SerializedName("ProductQuality")
private String productQuality;
@SerializedName("Invoice")
private String invoice;
@SerializedName("Other")
private String other;
public String getUnReceive() {
return unReceive;
}
public void setUnReceive(String unReceive) {
this.unReceive = unReceive;
}
public String getReturnGoods() {
return ReturnGoods;
}
public void setReturnGoods(String returnGoods) {
ReturnGoods = returnGoods;
}
public String getShopService() {
return ShopService;
}
public void setShopService(String shopService) {
ShopService = shopService;
}
public String getProductQuality() {
return productQuality;
}
public void setProductQuality(String productQuality) {
this.productQuality = productQuality;
}
public String getInvoice() {
return invoice;
}
public void setInvoice(String invoice) {
this.invoice = invoice;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈