Getting unexpected JDWP: 103 error while trying to upload an image using a modification to an api Android server?

I get the following error message 103 when I try to upload an image on the api server using the multipart modification, the api receives the api token, "_method": "PUT" and the image URL as parameters, when receiving the response it receives full JSON, but with the previous link or link by default, the currently selected image does not load, everything else is optional, any help will be appreciated, the code is listed below.

The main method is POST, but "_method": the "PUT" parameter allows you to upload an image, without _method it becomes a POST message, and every other parameter becomes mandatory, check the image.

com.sun.jdi.InternalException: Unexpected JDWP error: 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

+4
source share
1 answer

I think your problem is that you forgot to add an adapter to call for the modified object. In your class, RestClientyou should put something like this:

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:

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

Hope this helps.

Greetings.

+4
source

Source: https://habr.com/ru/post/1676192/


All Articles