Android Studio:Retrofit2:图像和字符串的多部分连接失败 [英] Android Studio: retrofit2: multipart connection failure with image and strings

查看:127
本文介绍了Android Studio:Retrofit2:图像和字符串的多部分连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void Dialog_profile_pic() {
    // create upload service client
    File file = new File(selectedImagePath);

    // create RequestBody instance from file
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // MultipartBody.Part is used to send also the actual file name
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("memFile", file.getName(), requestFile);

    // add another part within the multipart request
    RequestBody description =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), ApiResultCode.getApiKey());

    RequestBody description2 =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), ApiResultCode.getApiType());

    Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
            .loginPicture(description, description2, body);

    loginPictureCall.enqueue(new Callback<LoginPicture>() {
        @Override
        public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
            //CONNECTION SUCCESS
            LoginPicture NewUser = response.body();
            if (NewUser.getResponsedata().getResultCode() == 100) {
                Log.e("DEBUG", "CONNECTION result: CONGRATS");
            } else {

                Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
            }
        }

        @Override
        public void onFailure(Call<LoginPicture> call, Throwable t) {
            //CONNECTION FAIL
            Log.e("DEBUG", "CONNECTION result: FAIL" );
        }
    });
}

\

public interface ApiInterface {
@Multipart
    @POST("/memberController/joinUploadProfile.json")
    Call<LoginPicture> loginPicture(@Part("apiKey") RequestBody apiKey, @Part("apiType") RequestBody apiType, @Part("memFile") MultipartBody.Part file); //multi part

\ 我正在尝试使用Retrofit2传递两个字符串参数和一个图像文件,但是我无法获得连接.任何人都可以帮助我找到需要做的事情吗?我已经为此苦苦挣扎了两天了.

\ I am trying to pass in two string parameters and an image file using retrofit2 but I am failing to get a connection. Anyone can help me find what I need to do? I have been struggling over this for 2 days now.

推荐答案

//在请求客户端API接口中

//In Request Client API Interface

@Multipart
    @POST("URL.json")
    Call<LoginPicture> loginPicture(@PartMap() Map<String, RequestBody> mapPhoto); //multi part

//活动中

private void Dialog_profile_pic(final Uri selectedImageUri) {
    // create upload service client
    File file = new File(selectedImagePath);
    HashMap<String, RequestBody> map = new HashMap<>();
    RequestBody description =
            RequestBody.create(
                    MediaType.parse("text/plain"),"content1");
    RequestBody description2 =
            RequestBody.create(
                    MediaType.parse("text/plain"), "content2");

    // create RequestBody instance from file
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("image/jpeg"), file);
    map.put("memFile\"; filename=\""+file.getName(),requestFile);
    map.put("apiKey",description);
    map.put("apiType", description2);

    Call<LoginPicture> loginPictureCall = RequestClient.getInstance()
            .loginPicture(map);
    loginPictureCall.enqueue(new Callback<LoginPicture>() {
        @Override
        public void onResponse(Call<LoginPicture> call, Response<LoginPicture> response) {
                Picasso.with(Activity_create.this).load(NewUser.getResponsedata().getResultObject()).into(iv_profile_pic);
            } else {
                Log.e("DEBUG", "CONNECTION result: " + NewUser.getResponsedata().getResultCode() + NewUser.getResponsedata().getResultMessage());
            }
        }

        @Override
        public void onFailure(Call<LoginPicture> call, Throwable t) {
            //통신 실패 시
            Log.e("DEBUG", "CONNECTION result: FAIL");
        }
    });
}

//现在可以使用

这篇关于Android Studio:Retrofit2:图像和字符串的多部分连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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