OKHttp 上传图片 上传多张图片

public class OkBaseHttpImpl{

//使用单例模式创建对象
private OkHttpClient okHttpClient;
public static final MediaType JSON=MediaType.parse("application/json; charset=utf-8");
//构造函数私有化
private OkBaseHttpImpl(){
    okHttpClient = new OkHttpClient();
}

private static OkBaseHttpImpl okHttpUtils;

//提供一个公共的、静态的、返回值类型是当前本类的对象
public static OkBaseHttpImpl getInstance(){
    if(okHttpUtils == null){
        synchronized (OkBaseHttpImpl.class){
            if(okHttpUtils == null)
                okHttpUtils = new OkBaseHttpImpl();
        }
    }
    return okHttpUtils;
}


@Override
public  void upload(String url, Map map, File file, NetWorkCallBack callback) {
    // MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

    RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
    RequestBody requestBody=new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file",file.getName(),fileBody).build();
    Request request=new Request.Builder().url(url).post(requestBody).build();

    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, final IOException e) {

            //(BaseActivity)App.context
            App.mBaseActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.onError(404,e.getMessage());
                }
            });

        }

        @Override
        public void onResponse(final Call call, Response response) throws IOException {
            final String result=response.body().string();
            App.mBaseActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.onSuccess(getGeneric(result,callback));
                }
            });
        }
    });
}


private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

@Override
public  void uploadMany(String url, Map params, List files, NetWorkCallBack callback) {

    MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
    multipartBodyBuilder.setType(MultipartBody.FORM);
    //遍历map中所有参数到builder
    if (params != null){
        for (String key : params.keySet()) {
            multipartBodyBuilder.addFormDataPart(key, params.get(key));
        }
    }

    //遍历paths中所有图片绝对路径到builder,并约定key如“upload”作为后台接受多张图片的key
    if (files != null){
        for (File file : files) {
            multipartBodyBuilder.addFormDataPart("files", file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
        }
    }

    //构建请求体
    RequestBody requestBody = multipartBodyBuilder.build();

    Request.Builder RequestBuilder = new Request.Builder();
    RequestBuilder.url(url);// 添加URL地址
    RequestBuilder.post(requestBody);
    Request request = RequestBuilder.build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            App.mBaseActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.onError(404,e.getMessage());
                }
            });
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String result=response.body().string();
            App.mBaseActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.onSuccess(getGeneric(result,callback));
                }
            });
        }
    });
}
}

 

你可能感兴趣的:(android)