1

**In android application API not working. In application when I click on the call button on that time call will be recorded and send to server using retrofit. but I check on postman API working completely. What should I do? **

public class ApiClient {

public static final String BASE_URL = "BASE URL";

private static Retrofit retrofit = null;


public static Retrofit getClient() {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

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

    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
    return retrofit;
}

}

**Interface **

 //upload recorder file
@Multipart
@Headers({"Content-Type:application/json",
        "ENCTYPE:multipart/form-data",
        "cache-control: no-cache"
})
@POST ("UploadToServer")
Call<UploadDataAudio> uploadRecordingFile(@Part MultipartBody.Part AudioComment);

Calling api from MainActivity

public void updateRecordingFileName(String idoc_id, String filename) {
    // Showing progress dialog
        Log.e("Audiosavefilename ->",""+filename);

    File file=new File(filename);
    Log.e("Audio path",""+file.getAbsolutePath());
    Log.e("audio size",""+file.getAbsoluteFile().lastModified());


    String mimeType= URLConnection.guessContentTypeFromName(file.getName());



    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data") , file);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("uploaded_file" , file.toString() , requestBody);


    final ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);
    pDialog.show();

    APIInterface apiService = ApiClient.getClient().create(APIInterface.class);
    Call<UploadDataAudio> call = apiService.uploadRecordingFile(fileToUpload);
   Log.e("Audio url",""+ call.request().url().toString());
    call.enqueue(new Callback<UploadDataAudio>() {
        @Override
        public void onResponse(Call<UploadDataAudio> call, Response<UploadDataAudio> response) {
            Log.i("RETROFIT", "onResponse Called" + response.code());
            UploadDataAudio dataaudio=response.body();
            Log.e("RESPONSE",""+dataaudio.getResponseData().toString());
            Log.i("response",new Gson().toJson(dataaudio));
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }

        }

        @Override
        public void onFailure(Call<UploadDataAudio> call, Throwable t) {
            // Log error here since request failed
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
        }
    });

**In postman complete working here piture ** enter image description here

**When I upload an audio file on that time.this message show in debug mode **

java.net.ProtocolException: expected 3286 bytes but received 3699

Jigish
  • 221
  • 2
  • 9

1 Answers1

0

Did you try to by changing the read and write time out value ? Change the time out, if not working and see:-

public class RetrofitClient {
    static int cacheSize = 10 * 1024 * 1024; // 10 MB
    static Cache cache = new Cache(getCacheDir(), cacheSize);
    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .connectTimeout(200, TimeUnit.SECONDS)
                .readTimeout(200,TimeUnit.SECONDS)
                .build();
        if (retrofit == null) {

            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl).client(okHttpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
Android
  • 1,420
  • 4
  • 13
  • 23
Shyak Das
  • 119
  • 1
  • 5