Retrofit2.0:上傳圖片到PHP

雨幕青山發表於2017-04-24

上傳圖片到PHP

關於Retrofit2.0+Rxjava,道長先只說一下上傳圖片到PHP,至於其他的可以先看一下CaBiao的部落格,這裡面關於Retrofit2.0+Rxjava的使用講的比較詳細。
新增介面:

    /**
     * 上傳圖片
     * @return
     */
    @Multipart
    @POST(UrlContanier.ImgUpFile)
    Observable<HttpResult<UploadPicResponse>> uploadPicData(@PartMap Map<String, RequestBody> reqMessageBody);


實現上傳方法:
道長這裡把每一個請求引數例項化為RequestBody,然後把例項化後的實體放到Map

 public void uploadPicData(String picPath){
        String fileKey = "files";
        Map<String, RequestBody> clubUploadPicRequest = getBasePhpRequest();
        RequestBody headpic = RequestBody.create(MediaType.parse("multipart/form-data"), new File(picPath));
        fileKey += "\"; filename=\"" + new File(picPath).getName();
        clubUploadPicRequest.put(fileKey, headpic);
        RequestBody type = RequestBody.create(MediaType.parse("form-data"), "3");
        clubUploadPicRequest.put("type" , type);
        RequestBody liveid = RequestBody.create(MediaType.parse("form-data"), activeid);
        clubUploadPicRequest.put("liveid" , liveid);
        appAction.uploadPicData(new CallBack<UploadPicResponse>(ClubActivityDetailActivity.this) {
            @Override
            public void onCompleted() {
                SimpleHUD.dismiss();
            }
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                SimpleHUD.dismiss();
            }
            @Override
            public void onNext(UploadPicResponse uploadPicResponse) {
                String json = JsonUtil.GsonString(uploadPicResponse);
                LogHelper.v("lee", "uploadPicResponse:" + json);
            }
        }, clubUploadPicRequest);
    }
    public Map<String, RequestBody> getBasePhpRequest(){
        Map<String, RequestBody> request = new HashMap<>();
        RequestBody userid = RequestBody.create(MediaType.parse("form-data"), PreferenceHelper.get(ClubActivityDetailActivity.this, PreferenceHelper.PREFERENCE_LOGIN, "userid", 0) + "");
        request.put("userid", userid);
        RequestBody accessToken = RequestBody.create(MediaType.parse("form-data"),Config.ACCESSTOKEN);
        request.put("accessToken",accessToken);
        RequestBody clientVersion = RequestBody.create(MediaType.parse("form-data"),Config.CLIENTVERSION);
        request.put("clientVersion",clientVersion);
        RequestBody clientName = RequestBody.create(MediaType.parse("form-data"),Config.CLIENTNAME);
        request.put("clientName",clientName);
        RequestBody version = RequestBody.create(MediaType.parse("form-data"),Config.HTTP_VERSION);
        request.put("version",version);
        return request;
    }


這裡要注意兩點:
1.封裝圖片的實體的程式碼為:

RequestBody headpic = RequestBody.create(MediaType.parse("multipart/form-data"), new File(picPath));
        fileKey += "\"; filename=\"" + new File(picPath).getName();
        clubUploadPicRequest.put(fileKey, headpic);

其中fileKey的拼接是不可改動的,如果改動的話會導致上傳不成功。

2.封裝其他請求引數的實體的程式碼為:

RequestBody userid = RequestBody.create(MediaType.parse("form-data"), PreferenceHelper.get(ClubActivityDetailActivity.this, PreferenceHelper.PREFERENCE_LOGIN, "userid", 0) + "");
        request.put("userid", userid);

如果請求引數不是固定值,最好把引數設定為全域性變數,否則會例項化RequestBody不成功。希望這篇部落格能為你提供一些幫助。


相關文章