Retrofit上传图片

MyApiService

@Multipart
    @POST
    Observable getFile(@Url String url, @QueryMap Map map, @Part  MultipartBody.Part file);

RetrofitUtils

public void upImage(String url, Map map, MultipartBody.Part body,final HttpListener listener){
        Observer observer = new Observer() {
            @Override
            public void onCompleted() {
                Log.e("onCompleted", "onCompleted");
            }

            //网络处理失败
            @Override
            public void onError(Throwable e) {
                Log.e("onError", "onError" + e.getMessage());
                if (listener != null) {
                    listener.onError(e.getMessage());
                }
            }

            //网络处理成功
            @Override
            public void onNext(ResponseBody responseBody) {
                Log.d("onNext", "onNext");
                if (listener != null) {
                    try {
                        listener.onSuccess(responseBody.string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        };
        myApiService.getFile(url,map,body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

调用

final Map map = new HashMap<>();
        map.put("uid",uid+"");
        RetrofitUtils.getInstance().upImage(Contacts.USER_USER_URL, map, part, new RetrofitUtils.HttpListener() {
            @Override
            public void onSuccess(String jsonStr) {
                Log.d("成功",jsonStr);
//                HashMap map2 = new HashMap<>();
//                map2.put("uid",uid+"");
//                mIpresenter.startHttp(Contacts.URL_USER_INFO,map2, MyInfoBean.class);
            }

            @Override
            public void onError(String error) {

            }
        });

    }

你可能感兴趣的:(Retrofit上传图片)