我正在尝试使用改装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);
}
}
但是我在使用OkHttpClient时遇到错误。说不能解析Builder类的构建器。
第3。 build.gradle文件:
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'
}
请指导我如何使用Retrofit 2.0将文件上传到服务器。
我正在使用this教程将文件上传到服务器。在这里,我需要创建一个Service Generator类,如下所示:
我现在已经编辑了我的代码。 请不要考虑上面提到的代码。现在考虑以下事项: 已编辑的代码:
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));
}
预期的JSON响应:
{
"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"
}
在解析JSON响应时,我得到响应字符串中指定的状态代码200。但是我无法从upload_data对象接收file_name。我无法知道我在做错误的地方。请帮我解决问题。
要使用的网址是:
http://phpstack-11819-25991-62288.cloudwaysapps.com/webservice/media/?user_id=<user_id>&type=photo&active=1&access_token=<access_token>
图像需要作为multipart / form-data上传。 文件'name'属性必须是'image'。