javaandroidretrofitretrofit2mobile-development

JSON data was not fully consumed


While sending the image with multipart using retrofit library getting this error.

I have checked many solution which are available in Stack Overflow but not any of them work for me.

Here I am providing you my code:

public class ApiHandler {

    private static final String BASE_URL = "https://aaplasevak.com/";

    private static Retrofit retrofit;

    public static Retrofit getRetrofitInstance() {


        if(retrofit == null){

            OkHttpClient client = new OkHttpClient.Builder().build();

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

            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }

        return retrofit;
    }
   
}

My Api Interface class

    @Multipart
    @POST("whatsapp_api.php")
    Call<WhatsAppApiResponseData> sentWhatsAppMessage(
            @Part("series_id") RequestBody seriesId,
            @Part("colony_id") RequestBody  colonyId,
            @Part("row_id") RequestBody  rowId,
            @Part("watersupply_id") RequestBody  waterSupplyId,
            @Part("constituency") RequestBody  constituency,
            @Part("city_village") RequestBody  cityVillage,
            @Part("zone") RequestBody  zone,
            @Part("prabhag_ward") RequestBody  prabhagWard,
            @Part("template_desc") RequestBody  templateDesc,
            @Part MultipartBody.Part file
    );

API Implementation code


private void sentWhatsAppMessage() {
        ApiInterface apiInterface = getRetrofitInstance().create(ApiInterface.class);

        RequestBody seriesId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_series.getSelectedItemPosition()));
        RequestBody colonyId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_colony.getSelectedItemPosition() + 1));
        RequestBody rowId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_row.getSelectedItemPosition() + 1));
        RequestBody waterSupplyId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_water_Supply.getSelectedItemPosition()));
        RequestBody zoneId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_zone.getSelectedItemPosition()));
        RequestBody wardId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_ward.getSelectedItemPosition()));
        RequestBody constituencyId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_constituency.getSelectedItemPosition()));
        RequestBody cityVillageId = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(spinner_city_village.getSelectedItemPosition()));
        RequestBody message = RequestBody.create(MediaType.parse("multipart/form-data"),  etMessage.getText().toString());


        File file = new File(getRealPathFromURI(selectedUri));
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);


        Call<WhatsAppApiResponseData> call = apiInterface.sentWhatsAppMessage(seriesId,colonyId,rowId,waterSupplyId,constituencyId,cityVillageId,zoneId,wardId,message,body);
        //Log.d("Api Response", "WhatsApp: " + whatsAppApiBody.toString());


        call.enqueue(new Callback<WhatsAppApiResponseData>() {
            @Override
            public void onResponse(Call<WhatsAppApiResponseData> call, retrofit2.Response<WhatsAppApiResponseData> response) {
                if (response.isSuccessful()) {
                    WhatsAppApiResponseData responseData = response.body();
                    String status = responseData.toString();
                    Toast.makeText(WhatsappActivity.this, "Success!! " + status, Toast.LENGTH_SHORT).show();
                    Log.d("Api Response","data: " + status);
                    finish();
                } else {
                    Toast.makeText(WhatsappActivity.this, "Response Error..!!", Toast.LENGTH_SHORT).show();
                    Log.e("Api Response", "Response Error..");
                }
            }

            @Override
            public void onFailure(Call<WhatsAppApiResponseData> call, Throwable throwable) {
                Log.e("Api Response", "Error.." + throwable.getLocalizedMessage());
                Toast.makeText(WhatsappActivity.this, "Error.." + throwable.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                throwable.printStackTrace();
            }
        });

    }

getRealPathFromUri Funtion

 private String getRealPathFromURI(Uri contentUri) {

        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        cursor.close();
        return filePath;

    }

While testing from the Postman API work fine, but from here I am facing, issue, always showing:

Json Data Was not fully consumed


Solution

  • The api is working now, the only thing I need to change is

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

    // Need to replace the "multipart/form-data" with "image/jpeg" for image