I am trying to upload image using retrofit 2.0.
1.APIServiceInterface.java
public interface APIServiceInterface {
@POST("almabay_oauth/authorize")
Call<ResponseLogin> login(@Body UserLogin userLogin); // Sending JSON object in login method and response will be stoed in POJO ResponseLogin
// /webservice/media?user_id=<user_id>&type=photo&active=1&access_token=<access_token>
//Uploading Image to server
@Multipart
@POST("webservice/media/")
Call<ResponseUploadImage> uploadImage(@Query("user_id") String userID,
@Query("type") String type,
@Query("active") Long active,
@Query("access_token") String accessToken,
@Part("myfile\"; filename=\"image.png\" ") RequestBody file
);
}
2.ServiceGenerator.java
public class ServiceGenerator {
public static final String API_BASE_URL = "http://phpstack-11819-25991-62288.cloudwaysapps.com/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
But here i am getting error while using OkHttpClient. Builder which says that can not resolve Builder class.
3. build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.7.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
}
Please guide me how can I upload file to server using Retrofit 2.0.
I am using this tutorial to upload file to server. Here I need to create a Service Generator class as given below:
I have edited my code now. Please don't consider the above mentioned code. Consider the following now: Edited Code:
1.build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.picasso:picasso:2.5.2'
}
2.ServiceGenerator.java
public class ServiceGenerator {
public static final String API_BASE_URL = "http://phpstack-11819-25991-62288.cloudwaysapps.com/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
3.WelcomeActivity.java
APIServiceInterface service =
ServiceGenerator.createService(APIServiceInterface.class);
String descriptionString = "hello, this is description speaking";
RequestBody description =
RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
File file = new File(path);
RequestBody requestBody =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
String type = "photo";
int active = 1;
Call<ResponseUploadImage> call = service.uploadImage(userID, type, active, accessToken, requestBody, description);
call.enqueue(new Callback<ResponseUploadImage>() {
@Override
public void onResponse(Response<ResponseUploadImage> response) {
Log.e("Status Code", String.valueOf(response.code()));
if (response.code() == 200) {
ResponseUploadImage responseUploadImage = response.body();
String statusCode = responseUploadImage.getStatusCode();
Log.e("StatusCode", statusCode);
Detail detail = responseUploadImage.getDetail();
UploadData uploadData = detail.getUploadData();
String fileName = uploadData.getFileName();
Log.e("FileName",fileName);
}
}
@Override
public void onFailure(Throwable t) {
Log.e("Response", "failure");
Log.e("StackTrace", String.valueOf(t));
}
Expected JSON Response:
{
"detail": {
"upload_data": {
"file_name": "AlmaBay_Homepage_Screenshot.png",
"file_type": "image\/png",
"file_path": "\/var\/www\/html\/ecampusbay\/common\/marvel\/photos\/2015\/10\/20\/",
"full_path": "\/var\/www\/html\/ecampusbay\/common\/marvel\/photos\/2015\/10\/20\/AlmaBay_Homepage_Screenshot.png",
"raw_name": "AlmaBay_Homepage_Screenshot",
"orig_name": "AlmaBay_Homepage_Screenshot.png",
"client_name": "AlmaBay Homepage Screenshot.png",
"file_ext": ".png",
"file_size": 5073.14,
"is_image": true,
"image_width": 1919,
"image_height": 899,
"image_type": "png",
"image_size_str": "width=\"1919\" height=\"899\""
}
},
"statusCode": "200"
}
While parsing the JSON response, I am getting the status code of 200 which is specified in the Response String. But I am not able to receive file_name from the upload_data object. I am not able to know where I am doing the mistake. Please help me to fix the problem.
URL to be used is :
http://phpstack-11819-25991-62288.cloudwaysapps.com/webservice/media/?user_id=<user_id>&type=photo&active=1&access_token=<access_token>
Image need to be uploaded as multipart/form-data. File ‘name’ attribute must be ‘image’.