OkHttp3最简单封装

一、使用

1,异步Get

String url="http://www.2014nut.com/app.php";

OkHttpManager.getAsyn(url, newOkHttpManager.ResultCallback() {

@Override

public void onError(Call call,Exception e) {

}

@Override

public void onResponse(Call call,Response response) {

}

});

2,异步Post

String url="http://www.2014nut.com/app.php";

Map map=newHashMap();

map.put("id","1");

OkHttpManager.postAsyn(url, newOkHttpManager.ResultCallback() {

@Override

public void onError(Call call,Exception e) {

}

@Override

public void onResponse(Call call,Response response) {

}

},map);

3,Post多文件上传,带参数

String url="http://www.2014nut.com/app.php";

Map map=newHashMap();

map.put("id","1");

OkHttpManager.postFileAsyn(url, newFile[]{}, newOkHttpManager.ResultCallback() {

@Override

public void onError(Call call,Exception e) {

}

@Override

public void onResponse(Call call,Response response) {

}

},map);

2、封装类:

importjava.io.File;

importjava.io.IOException;

importjava.net.FileNameMap;

importjava.net.URLConnection;

importjava.util.Map;

importokhttp3.Call;

importokhttp3.Callback;

importokhttp3.FormBody;

importokhttp3.MediaType;

importokhttp3.MultipartBody;

importokhttp3.OkHttpClient;

importokhttp3.Request;

importokhttp3.RequestBody;

importokhttp3.Response;

/**

* Created by Administrator on 2017/4/20 0020.

*/

public classOkHttpManager {

public static finalStringTAG="OkHttpManger";

private staticOkHttpClientokHttpClient;

public staticOkHttpManagermanager;

privateOkHttpManager() {

okHttpClient=newOkHttpClient();

}

public staticOkHttpManagergetInstance() {

if(manager==null) {

synchronized(OkHttpManager.class) {

if(manager==null) {

manager=newOkHttpManager();

}

}



}

returnmanager;

}

private void _getAsyn(String url, finalResultCallback callback) {

finalRequest request =newRequest.Builder()

.url(url)

.build();

deliveryResult(callback,request);

}

private void_postFileAsyn(String url,File[] files, finalResultCallback callback,Map map) {

MultipartBody.Builder builder =newMultipartBody.Builder().setType(MultipartBody.FORM);

//添加文件

if(files.length!=0) {

for(inti =0;i < files.length;i++) {

RequestBody fileBody = RequestBody.create(

MediaType.parse(getMediaType(files[i].getName())),files[i]);

builder.addFormDataPart("uploadfile",files[i].getName(),fileBody);

}

}

//添加参数

if(map !=null) {

for(Map.Entry entry : map.entrySet()) {

builder.addFormDataPart(entry.getKey(),entry.getValue());

}

}

Request request =newRequest.Builder().url(url).post(builder.build()).build();

okHttpClient.newCall(request).enqueue(newCallback() {

@Override

public voidonFailure(Call call,IOException e) {

callback.onError(call,e);

}

@Override

public voidonResponse(Call call,Response response)throwsIOException {

callback.onResponse(call,response);

}

});

}

private void_postAsyn(String url, finalResultCallback callback,Map map) {

FormBody.Builder build =newFormBody.Builder();

if(map !=null) {

//增强for循环遍历

for(Map.Entry entry : map.entrySet()) {

build.add(entry.getKey(),entry.getValue());

}

}

FormBody formBody = build.build();

Request request = buildPostRequest(url,formBody);

deliveryResult(callback,request);

}

privateRequestbuildPostRequest(String url,FormBody formBody) {

return newRequest.Builder()

.url(url)

.post(formBody)

.build();

}

/**

* 根据文件的名称判断文件的Mine值

*/

privateStringgetMediaType(String fileName) {

FileNameMap map = URLConnection.getFileNameMap();

String contentTypeFor = map.getContentTypeFor(fileName);

if(contentTypeFor ==null) {

contentTypeFor ="application/octet-stream";

}

returncontentTypeFor;

}

private voiddeliveryResult(finalResultCallback callback,Request request) {

okHttpClient.newCall(request).enqueue(newCallback() {

@Override

public voidonFailure(Call call,IOException e) {

callback.onError(call,e);

}

@Override

public voidonResponse(Call call,Response response)throwsIOException {

callback.onResponse(call,response);

}

});

}

public static voidgetAsyn(String url,ResultCallback callback) {

getInstance()._getAsyn(url,callback);

}

public static voidpostAsyn(String url,ResultCallback callback,Map params) {

getInstance()._postAsyn(url,callback,params);

}

public static voidpostFileAsyn(String url,File[] files,ResultCallback callback,Map params) {

getInstance()._postFileAsyn(url,files,callback,params);

}

public static abstract classResultCallback {

public abstract voidonError(Call call,Exception e);

public abstract voidonResponse(Call call,Response response);

}

}

你可能感兴趣的:(OkHttp3最简单封装)