Retrofit2:使用某些表单数据上传图像-Android

时间:2016-10-05 05:29:38

标签: android json image retrofit retrofit2

The PostMan screenshot

上面的截图包含来自服务器的Post方法和Response。 在这里我试图将带有一些数据的图像上传到我的服务器(从图库或相机中捕获图像)..

我试图通过retrofit2实现这一目标。 从互联网上尝试一些方法......但我无法正确地完成它

新改造2。所以我不能这样做。

  

如何创建retrofit2界面并处理响应..   请帮忙

     

编辑我的尝试

活动中的代码

  ProofAPI service =  retrofit.create(ProofAPI.class);
        File file=new File(sdCard.getAbsolutePath()+fNameAd);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("proof", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));

 HashMap<String, String> map = new HashMap<>();
        map.put("userId", user_id);
        map.put("proofDocId", idproofno);
        map.put("proofFilename", proofFilename);
        map.put("docType", docType);
    Call<ProofResp> call = service.uploadFileWithPartMap(map,filePart);
        call.enqueue(new Callback<ProofResp>() {
            @Override
            public void onResponse(Call<ProofResp> call, Response<ProofResp> response) {
                Toast.makeText(getApplicationContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<ProofResp> call, Throwable t) {
                Toast.makeText(getApplicationContext(),"fjkds",Toast.LENGTH_SHORT).show();
            }
        });
  

Retofit2界面 - &gt; ProofAPI

@Multipart
    @POST("rest-proof-upload")
    Call<ProofResp> uploadFileWithPartMap(
            @PartMap() Map<String, String> partMap,
            @Part MultipartBody.Part file
    );
  

我收到了失败消息。这是正确的方式..   任何人请解释正确的方法来实现它。

1 个答案:

答案 0 :(得分:3)

我做了类似下面的事情: -

我的界面

    @Multipart
    @POST("your_path")
    Call<ProofResp> uploadFileWithPartMap(@PartMap Map<String, RequestBody> params,
    @Part("profile_pic\"; filename=\"image.jpeg\"") RequestBody file);

现在我正在创建如下的

部分地图
 HashMap<String, RequestBody> map = new HashMap<>();
 map.put("userId", toRequestBody(userId));

我的RequestBody功能如下: -

public RequestBody toRequestBody(String value) {
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
    return body;
}

现在为图像部分

RequestBody file=RequestBody.create(MediaType.parse("image/jpeg"),
createTempFile(your_bitmap));

现在是createTempFile功能

private File createTempFile(Bitmap bitmap) {
        File file = new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES), System.currentTimeMillis()
                + "_image.jpeg");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();
//write the bytes in file

        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

现在将此文件地图发送到您的interface,您将收到回复。希望上述提示可以帮助您解决问题。< / p>