简体   繁体   English

获取意外的JDWP错误:103尝试使用改造上传图像到api服务器android?

[英]Getting a Unexpected JDWP Error: 103 while trying to upload an image using retrofit to an api server android?

I'm getting the following 103 error message while trying to upload an image on the api server using retrofit multipart, the api receives an api token, "_method":"PUT" and image url as parameters, on response receives the complete JSON but with the previous or default image link, the current selected image does not upload, everything else is optional, any help would be appreciated, code is listed below thanks. 尝试使用改装多部件在api服务器上传图像时,我收到以下103错误消息,api收到api令牌,“_ method”:“PUT”和图像url作为参数,响应时收到完整的JSON但是使用上一个或默认图像链接,当前所选图像不上传,其他一切都是可选的,任何帮助将不胜感激,代码列在下面谢谢。

Also main method is POST but "_method":"PUT" parameter lets you upload the image, without _method it becomes a POST message and every other parameter than becomes compulsory, please check the image. 主要的方法是POST,但“_method”:“PUT”参数允许你上传图像,没有_method它变成一个POST消息和所有其他参数而不是强制,请检查图像。

com.sun.jdi.InternalException: Unexpected JDWP Error: 103 com.sun.jdi.InternalException:意外的JDWP错误:103

Interface: 接口:

public interface UserSignUpClient {

@POST("account")
Call<AuthenticationAccountCreationResponse> createAccount(@Body UserSignUp userSignUp);

@Multipart
@PUT("account")
Call<UserInfo> postImage(@Header("Authorization") String headerValue, @PartMap Map<String, String> map, @PartMap Map<String, File> imageMap, @Part MultipartBody.Part image);

@FormUrlEncoded
@POST("account")
Call<UserInfo> updateUser(@Header("Authorization") String headerValue, @FieldMap Map<String, String> map);
}

Retrofit builder class: 改造生成器类:

public class RestClient {

private UserSignInClient userSignInClient;
private UserSignUpClient userSignUpClient;
private UserInfoClient userInfoClient;

public RestClient(String baseUrlLink){

    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrlLink)
            .addConverterFactory(GsonConverterFactory.create(gson)).build();

    if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_TOKEN)){

        userSignInClient = retrofit.create(UserSignInClient.class);
    } else if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_ACCOUNT_CREATION)) {

        userSignUpClient = retrofit.create(UserSignUpClient.class);
    } else if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_USER_INFO)){

        userInfoClient = retrofit.create(UserInfoClient.class);
    }
}

public UserSignInClient getUserSignInClient() {
    return userSignInClient;
}

public UserSignUpClient getUserSignUpClient() {
    return userSignUpClient;
}

public UserInfoClient getUserInfoClient() {
    return userInfoClient;
}
}

Main Class: 主类:

userImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            galleryIntent.setType("*/*");
            startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
        }
    });

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {

        Uri selectedImageUri = data.getData();
        String filePath = FileUtils.getPath(this, selectedImageUri);
        file = new File(filePath);
        image_name = file.getName();

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            } else {

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
            }
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                StringBuilder stringBuilder = new StringBuilder(AUTHORIZATION);

                sharedPreferences = this.getSharedPreferences(getResources().getString(R.string.token), 0);
                stringBuilder.append(sharedPreferences.getString(getResources().getString(R.string.token), ""));

                RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
                MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestBody);

                UserUpdate userUpdate = new UserUpdate(file);

                methodMap.put("_method","PUT");
                imageMap.put("image", file);

                Call<UserInfo> uploadImage = new RestClient(CONSTS.BASE_URL_ADDRESS_ACCOUNT_CREATION).getUserSignUpClient()
                        .postImage(stringBuilder.toString(), methodMap, imageMap, body);

                uploadImage.enqueue(new Callback<UserInfo>() {
                    @Override
                    public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {

                        if (response.isSuccessful()) {

                            Picasso.with(DetailActivity.this).load("http://ec2-35-161-195-128.us-west-2.compute.amazonaws.com/" + response.body().getImage()).into(userImage);

                        }
                    }

                    @Override
                    public void onFailure(Call<UserInfo> call, Throwable t) {

                        Toast.makeText(DetailActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
}
}

postman api image = https://i.imgur.com/AoCFZI6.png postman api image = https://i.imgur.com/AoCFZI6.png

I think your problem is you forgot to add a call adapter to the retrofit object. 我认为您的问题是您忘记向改装对象添加呼叫适配器 Within your RestClient class you must put something like this: 在您的RestClient类中,您必须输入以下内容:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrlLink)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();

Also you need to add this dependency inside build.gradle : 您还需要在build.gradle添加此依赖build.gradle

compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0"

I hope it helps. 我希望它有所帮助。

Cheers. 干杯。

我将Retrofit依赖性更新为“2.4.0”并解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM