I successfully created one demo to upload image via API. If I select mobile screenshot, it is happily working. but If I choose large file then it's not working and the exception is: 413
onResponse: [size=208 text=<html>\r\n<head><title>413 Request Entity Too Large</title></head>…]
So, I searched this problem and found thousands of solution. Everyone is saying same thing. Please make a configuration on server side to extend limit
But my sweet and simple question is: Whichever image is not working with retrofit, same I tried with postman and it is working! How? And it means there is no additional need to do configuration in web server.
Now, I've doubt that might be Retrofit itself is not allowing this much large files. Then should I try with AsyncTask or may be Volley?
Code: Retrofit Client:
package com.androidbuts.api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* @author Pratik Butani
*/
public class RetroClient {
/**
* Upload URL of your folder with php file name...
* You will find this file in php_upload folder in this project
* You can copy that folder and paste in your htdocs folder...
*/
private static final String ROOT_URL = "https://b2cprintappstg.e-arc.com/microservice/";
/**
* Get Retro Client
*
* @return JSON Object
*/
static Gson gson = new GsonBuilder()
.setLenient()
.create();
public RetroClient() {
}
private static Retrofit getRetroClient() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.client(getHeader())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
public static ApiService getApiService() {
return getRetroClient().create(ApiService.class);
}
public static OkHttpClient getHeader() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okClient = new OkHttpClient.Builder()
.readTimeout(100, TimeUnit.SECONDS)
.connectTimeout(100, TimeUnit.SECONDS)
.writeTimeout(100, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.addNetworkInterceptor(
new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = null;
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("ClientID", "xxxxxx")
.addHeader("x-access-token", "3uYch6X1i2OuRd3DvBPvvwiMRqPboBdRt/PbSiP0KFB4eaQQFg==")
.addHeader("PartnerID", "fV17XLswDUwlU9q9ofx4pkhezw==");
request = requestBuilder.build();
return chain.proceed(request);
}
})
.build();
return okClient;
}
}
API service:
public interface ApiService {
/*
Retrofit get annotation with our URL
And our method that will return us the List of Contacts
*/
@Multipart
@POST("user/new/api/design/files")
Call<ResponseBody> uploadImage(@Part MultipartBody.Part file,
@Part("design_id ") String designId);
}