Retrofit2:使用某些表格数据Android上载图像 [英] Retrofit2: Upload an Image with some Form Data-Android

查看:161
本文介绍了Retrofit2:使用某些表格数据Android上载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面的屏幕快照包含Post方法和服务器的响应. 在这里,我试图将包含一些数据的图像上传到我的服务器(从图库或照相机中捕获图像).

The above screenshot contain the Post method and Response from the server.. here i am trying to upload an image with some data to my server(catch the image from gallery or camera)..

我正在尝试通过Retrofit2实现这一目标. 从Internet尝试一些方法...但是我无法找到正确的方法

I am trying to achieve this via retrofit2. trying some methods from Internet... but i can't get the right way to done it

改造的新功能2.所以我不能做到这一点.

new in retrofit2. so I cant done this.

如何创建retrofit2接口并处理响应. 请帮助

How to Create retrofit2 interface and handle the response.. please help

编辑我的尝试

活动中的代码

  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接口-> ProofAPI

Retofit2 Interface -> ProofAPI

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

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

and I get the failure message. Is this the right way .. Anyone please explain the correct way to achive it.

推荐答案

我做了如下操作:-

我的界面

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

现在我要像下面那样创建零件图

Now I am creating partmap like below

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

我的RequestBody函数如下:-

My RequestBody function is below :--

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函数

Now the createTempFile funtion

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,您将得到答复.希望以上想法可以帮助您解决问题.

Now send this file and map to your interface and you will get the response.Hope this above idea will help you to solve your problem.

这篇关于Retrofit2:使用某些表格数据Android上载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆