一.okhttp 的基本使用:
- get/post 请求
- 文件上传/下载
- 加载图片
- 支持请求回调,直接返回对象/集合
- 支持session的保存
二.简单Get使用
//最简单的get请求
@Test
public void okhttpGet() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("https://github.com/hongyangAndroid")
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
response.body().string();//得到字符串
response.body().bytes();//二进制
response.body().bytesStream();//inputStream
}
});
}
以上是一个get请求,构造一个Request对象,给定参数url,method,header之类的设置
以此request为参数得到一个Call对象 ,然后调用enqueue进入栈中,同时call带有的方法有cancel()和execute()等
最后以异步的方式执行请求,并在callback中即可得到结果
成功回调的字符串是response;这里应该注意onResponse执行的线程并不是U线程
可以直接使用excute()返回response
Response response = call.excute()
三.Post请求
@Test
public void okhttpPost() throws IOException {
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody build = new FormBody.Builder()
.add("key", "value")
.build();
Request request = new Request.Builder()
.url("")
.post(build)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
}
四。文件上传
OkHttpClient okHttpClient=new OkHttpClient();
File file = new File(Environment.getExternalStorageDirectory(), "test.txt");
RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addPart(Header对象,body)
//.addFormDataPart("XXX","XXX",body)
.build();
Request request = new Request.Builder()
.url("XXXX")
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback(){ });
五.封装
由于以上方法使用OKhttp非常不方便,现在使用自定义OkhttpClientManager ;洪洋大神使用的是OKhttp2,我使用okhttp3 对进行封装,封装之后可以进行较为简单的操作;
你只需要
OkHttpClientManager .xxx : 就可以使用okhttp进行各种get,post,同步,异步,文件图片的上传和下载等等
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;
import com.xiaodou.myapplication.activity.utils.ImageUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttpClientManager {
private static OkHttpClientManager mInstance;
private OkHttpClient mOkHttpClient;
private Handler mDelivery;
private Gson mGson;
private OkHttpClientManager() {
mOkHttpClient = new OkHttpClient();
mDelivery = new Handler(Looper.getMainLooper());
mGson = new Gson();
}
public static OkHttpClientManager getInstance() {
if (mInstance == null) {
synchronized (OkHttpClientManager.class) {
if (mInstance == null) {
mInstance = new OkHttpClientManager();
}
}
}
return mInstance;
}
/**
* 通过Get请求
*
* @param url
* @return
* @throws IOException
*/
private Response _getAsyn(String url) throws IOException {
Request build = new Request.Builder().url(url).build();
Response execute = mOkHttpClient.newCall(build).execute();
return execute;
}
/**
* 同步的Get请求
*
* @param url
* @return
* @throws IOException
*/
private String _getAsString(String url) throws IOException {
Response response = _getAsyn(url);
return response.body().string();
}
/**
* 异步的get请求,并做解析
*
* @param url
* @param callback
*/
private void _getAsyn(String url, final ResultCallback callback) {
Request request = new Request.Builder().url(url).build();
deliveryResult(callback, request);
}
/**
* 同步的post
*
* @param url
* @param params
* @return
*/
private Response _post(String url, Param... params) throws IOException {
Request request = buildPostRequest(url, params);
Response response = request(request);
return response;
}
/**
* 异步的post请求
*
* @param url
* @param callback
* @param params
*/
private void _postAsyn(String url, final ResultCallback callback, Param... params) {
Request request = buildPostRequest(url, params);
deliveryResult(callback, request);
}
//Asyn 就是 异步的意思
/**
* 异步的post请求
*
* @param url
* @param callback
* @param params
*/
private void _postAsyn(String url, final ResultCallback callback, Map<String, String> params) {
Param[] paramsArr = map2Params(params);
Request request = buildPostRequest(url, paramsArr);
deliveryResult(callback, request);
}
/**
* 同步基于post的文件上传
*
* @param url
* @param files
* @param fileKeys
* @param params
* @return
* @throws IOException
*/
private Response _post(String url, File[] files, String[] fileKeys, Param... params) throws IOException {
Request request = buildMultipartFormRequest(url, files, fileKeys, params);
return request(request);
}
private Response _post(String url, File file, String fileKey) throws IOException {
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, null);
return request(request);
}
private Response _post(String url, File file, String fileKey, Param... params) throws IOException {
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, params);
return request(request);
}
private Response request(final Request request) throws IOException {
return request(request);
}
/**
* 异步基于post的文件上传
*
* @param url
* @param callback
* @param files
* @param fileKeys
* @param params
* @throws IOException
*/
private void _postAsyn(String url, final ResultCallback callback, File[] files, String[] fileKeys, Param... params) throws IOException {
Request request = buildMultipartFormRequest(url, files, fileKeys, params);
deliveryResult(callback, request);
}
private void _postAsyn(String url, final ResultCallback callback, File[] files, String[] fileKeys) throws IOException {
Request request = buildMultipartFormRequest(url, files, fileKeys, null);
deliveryResult(callback, request);
}
/**
* 异步下载文件
*
* @param url
* @param destFileDir
* @param callback
*/
private void _downloadAsyn(final String url, final String destFileDir, final ResultCallback callback) {
final Request request = new Request.Builder()
.url(url)
.build();
final Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
sendFailedStringCallback(request, e, callback);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
File file = new File(destFileDir, getFileName(url));
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
} catch (Exception e) {
sendFailedStringCallback(response.request(), e, callback);
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
}
try {
if (fos != null) fos.close();
} catch (IOException e) {
}
}
}
});
}
/**
* 异步基于post的文件上传,单文件且携带其他form参数上传
*
* @param url
* @param callback
* @param file
* @param fileKey
* @param params
* @throws IOException
*/
private void _postAsyn(String url, ResultCallback callback, File file, String fileKey, Param... params) throws IOException {
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, params);
deliveryResult(callback, request);
}
private void _displayImage(final ImageView view, final String url, final int errorResId) {
final Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
setErrorResId(view, errorResId);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
try {
ImageUtils.ImageSize actualImageSize = ImageUtils.getImageSize(is);
ImageUtils.ImageSize imageViewSize = ImageUtils.getImageViewSize(view);
int inSampleSize = ImageUtils.calculateInSampleSize(actualImageSize, imageViewSize);
try {
is.reset();
} catch (IOException e) {
response = _getAsyn(url);
is = response.body().byteStream();
}
BitmapFactory.Options ops = new BitmapFactory.Options();
ops.inJustDecodeBounds = false;
ops.inSampleSize = inSampleSize;
final Bitmap bm = BitmapFactory.decodeStream(is, null, ops);
mDelivery.post(new Runnable() {
@Override
public void run() {
view.setImageBitmap(bm);
}
});
} catch (Exception e) {
setErrorResId(view, errorResId);
} finally {
if (is != null) try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
/**
* 同步的Post请求
*
* @param url
* @param params post的参数
* @return 字符串
*/
private String _postAsString(String url, Param... params) throws IOException {
Response response = _post(url, params);
return response.body().string();
}
private void _postAsyn(String url, ResultCallback callback, File file, String fileKey) throws IOException {
Request request = buildMultipartFormRequest(url, new File[]{file}, new String[]{fileKey}, null);
deliveryResult(callback, request);
}
/***********对外公布的方法******/
public static Response getAsyn(String url) throws IOException {
return getInstance()._getAsyn(url);
}
public static String getAsString(String url) throws IOException {
return getInstance()._getAsString(url);
}
public static void getAsyn(String url, ResultCallback callback) {
getInstance()._getAsyn(url, callback);
}
public static Response post(String url, Param... params) throws IOException {
return getInstance()._post(url, params);
}
public static String postAsString(String url, Param... params) throws IOException {
return getInstance()._postAsString(url, params);
}
public static void postAsyn(String url, final ResultCallback callback, Param... params) {
getInstance()._postAsyn(url, callback, params);
}
public static void postAsyn(String url, final ResultCallback callback, Map<String, String> params) {
getInstance()._postAsyn(url, callback, params);
}
public static Response post(String url, File[] files, String[] fileKeys, Param... params) throws IOException {
return getInstance()._post(url, files, fileKeys, params);
}
public static Response post(String url, File file, String fileKey) throws IOException {
return getInstance()._post(url, file, fileKey);
}
public static Response post(String url, File file, String fileKey, Param... params) throws IOException {
return getInstance()._post(url, file, fileKey, params);
}
public static void postAsyn(String url, ResultCallback callback, File[] files,
String[] fileKeys, Param... params) throws IOException {
getInstance()._postAsyn(url, callback, files, fileKeys, params);
}
public static void postAsyn(String url, ResultCallback callback, File file, String fileKey) throws IOException {
getInstance()._postAsyn(url, callback, file, fileKey);
}
public static void postAsyn(String url, ResultCallback callback, File file, String fileKey, Param... params) throws IOException {
getInstance()._postAsyn(url, callback, file, fileKey, params);
}
public static void displayImage(final ImageView view, String url, int errorResId) throws IOException {
getInstance()._displayImage(view, url, errorResId);
}
public static void displayImage(final ImageView view, String url) {
getInstance()._displayImage(view, url, -1);
}
public static void downloadAsyn(String url, String destDir, ResultCallback callback) {
getInstance()._downloadAsyn(url, destDir, callback);
}
private String getFileName(String path) {
int separatorIndex = path.lastIndexOf("/");
return (separatorIndex < 0) ? path : path.substring(separatorIndex + 1, path.length());
}
private void setErrorResId(final ImageView view, final int errorResId) {
mDelivery.post(new Runnable() {
@Override
public void run() {
view.setImageResource(errorResId);
}
});
}
private Request buildMultipartFormRequest(String url, File[] files, String[] fileKeys, Param[] params) {
params = validateParam(params);
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
for (Param param : params) {
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + param.key + "\""),
RequestBody.create(null, param.value));
}
if (files != null) {
RequestBody fileBody = null;
for (int i = 0; i < files.length; i++) {
File file = files[i];
String fileName = file.getName();
fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileName)), file);
//TODO 根据文件名设置contentType
builder.addPart(Headers.of("Content-Disposition",
"form-data; name=\"" + fileKeys[i] + "\"; filename=\"" + fileName + "\""),
fileBody);
}
}
RequestBody requestBody = builder.build();
return new Request.Builder()
.url(url)
.post(requestBody)
.build();
}
//猜测文件路径的 mumeType
private String guessMimeType(String path) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String contentTypeFor = fileNameMap.getContentTypeFor(path);
if (contentTypeFor == null) {
contentTypeFor = "application/octet-stream";
}
return contentTypeFor;
}
private Param[] map2Params(Map<String, String> params) {
if (params == null) return new Param[0];
int size = params.size();
Param[] res = new Param[size];
Set<Map.Entry<String, String>> entries = params.entrySet();
int i = 0;
for (Map.Entry<String, String> entry : entries) {
res[i++] = new Param(entry.getKey(), entry.getValue());
}
return res;
}
private Request buildPostRequest(String url, Param[] params) {
if (params == null) {
params = new Param[0];
}
FormBody.Builder builder = new FormBody.Builder();
for (Param param : params) {
builder.add(param.key, param.value);
}
return new Request.Builder()
.url(url)
.post(builder.build())
.build();
}
private Param[] validateParam(Param[] params) {
if (params == null)
return new Param[0];
else return params;
}
/**
* 根据泛型解析 响应的数据
*
* @param callback
* @param request
*/
private void deliveryResult(final ResultCallback callback, Request request) {
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
sendFailedStringCallback(request, e, callback);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String string = response.body().string();
if (callback.mType == String.class) {
sendSuccessResultCallback(string, callback);
} else {
Object json = mGson.fromJson(string, callback.mType);
sendSuccessResultCallback(json, callback);
}
} catch (IOException e) {
sendFailedStringCallback(response.request(), e, callback);
} catch (com.google.gson.JsonParseException e)//Json解析的错误
{
sendFailedStringCallback(response.request(), e, callback);
}
}
});
}
private void sendSuccessResultCallback(final Object object, ResultCallback callback) {
mDelivery.post(new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.onResponse(object);
}
}
});
}
public static abstract class ResultCallback<T> {
//这个位置就是
Type mType;
public ResultCallback() {
mType = getSuperclassTypeParameter(getClass());
}
private static Type getSuperclassTypeParameter(Class<? extends ResultCallback> subclass) {
Type superclass = subclass.getGenericSuperclass();
if (superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
}
public abstract void onError(Request request, Exception e);
public abstract void onResponse(T response);
}
public static class Param {
public Param() {
}
public Param(String key, String value) {
this.key = key;
this.value = value;
}
String key;
String value;
}
/**
* 失败的处理
*
* @param request
* @param e
* @param callback
*/
private void sendFailedStringCallback(final Request request, final Exception e, final ResultCallback callback) {
mDelivery.post(new Runnable() {
@Override
public void run() {
if (callback != null)
callback.onError(request, e);
}
});
}
}
图形的工具类
public class ImageUtils
{
/**
* 根据InputStream获取图片实际的宽度和高度
*
* @param imageStream
* @return
*/
public static ImageSize getImageSize(InputStream imageStream)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(imageStream, null, options);
return new ImageSize(options.outWidth, options.outHeight);
}
public static class ImageSize
{
int width;
int height;
public ImageSize()
{
}
public ImageSize(int width, int height)
{
this.width = width;
this.height = height;
}
@Override
public String toString()
{
return "ImageSize{" +
"width=" + width +
", height=" + height +
'}';
}
}
public static int calculateInSampleSize(ImageSize srcSize, ImageSize targetSize)
{
// 源图片的宽度
int width = srcSize.width;
int height = srcSize.height;
int inSampleSize = 1;
int reqWidth = targetSize.width;
int reqHeight = targetSize.height;
if (width > reqWidth && height > reqHeight)
{
// 计算出实际宽度和目标宽度的比率
int widthRatio = Math.round((float) width / (float) reqWidth);
int heightRatio = Math.round((float) height / (float) reqHeight);
inSampleSize = Math.max(widthRatio, heightRatio);
}
return inSampleSize;
}
/**
* 根据ImageView获适当的压缩的宽和高
*
* @param view
* @return
*/
public static ImageSize getImageViewSize(View view)
{
ImageSize imageSize = new ImageSize();
imageSize.width = getExpectWidth(view);
imageSize.height = getExpectHeight(view);
return imageSize;
}
/**
* 根据view获得期望的高度
*
* @param view
* @return
*/
private static int getExpectHeight(View view)
{
int height = 0;
if (view == null) return 0;
final ViewGroup.LayoutParams params = view.getLayoutParams();
//如果是WRAP_CONTENT,此时图片还没加载,getWidth根本无效
if (params != null && params.height != ViewGroup.LayoutParams.WRAP_CONTENT)
{
height = view.getWidth(); // 获得实际的宽度
}
if (height <= 0 && params != null)
{
height = params.height; // 获得布局文件中的声明的宽度
}
if (height <= 0)
{
height = getImageViewFieldValue(view, "mMaxHeight");// 获得设置的最大的宽度
}
//如果宽度还是没有获取到,憋大招,使用屏幕的宽度
if (height <= 0)
{
DisplayMetrics displayMetrics = view.getContext().getResources()
.getDisplayMetrics();
height = displayMetrics.heightPixels;
}
return height;
}
/**
* 根据view获得期望的宽度
*
* @param view
* @return
*/
private static int getExpectWidth(View view)
{
int width = 0;
if (view == null) return 0;
final ViewGroup.LayoutParams params = view.getLayoutParams();
//如果是WRAP_CONTENT,此时图片还没加载,getWidth根本无效
if (params != null && params.width != ViewGroup.LayoutParams.WRAP_CONTENT)
{
width = view.getWidth(); // 获得实际的宽度
}
if (width <= 0 && params != null)
{
width = params.width; // 获得布局文件中的声明的宽度
}
if (width <= 0)
{
width = getImageViewFieldValue(view, "mMaxWidth");// 获得设置的最大的宽度
}
//如果宽度还是没有获取到,憋大招,使用屏幕的宽度
if (width <= 0)
{
DisplayMetrics displayMetrics = view.getContext().getResources()
.getDisplayMetrics();
width = displayMetrics.widthPixels;
}
return width;
}
/**
* 通过反射获取imageview的某个属性值
*
* @param object
* @param fieldName
* @return
*/
private static int getImageViewFieldValue(Object object, String fieldName)
{
int value = 0;
try
{
Field field = ImageView.class.getDeclaredField(fieldName);
field.setAccessible(true);
int fieldValue = field.getInt(object);
if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE)
{
value = fieldValue;
}
} catch (Exception e)
{
}
return value;
}
}