我正在尝试将照片上传到服务器。 我的问题是我无法链接到服务器返回给我的这张图片。
public class RetrofitClient {
public static Retrofit createService(
String username, String password) {
String authToken = Credentials.basic(username, password);
return createService(authToken);
}
public static Retrofit createService(final String authToken) {
AuthenticationInterceptor interceptor = null;
if (!TextUtils.isEmpty(authToken))
interceptor = new AuthenticationInterceptor(authToken);
Gson gson = new GsonBuilder()
.setLenient()
.create();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(logging)
.retryOnConnectionFailure(true)
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS).build();
return new Retrofit.Builder()
.baseUrl(AppConstant.BASE_URL+"/")
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
public static RetroInterface getApiServices(String user_name, String passowrd) {
return createService(user_name, passowrd).create(RetroInterface.class);
}
}
RetroInterface.java
....
@Multipart
@POST("parents/profile/images")
Call<String>
uploadParentProfilePicture2(@Part MultipartBody.Part filePart);
....
Profile.java
....
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
RetroInterface retroInterface = RetrofitClient.getApiServices(getEmail(), getPassword());
Call<String> call = retroInterface.uploadParentProfilePicture(filePart);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
Log.d("ldsfjjlk", "response: " + response.body());
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
Log.d("ldsfjjlk", "Throwable: " + t.getLocalizedMessage());
}
});
....
HttpLoggingInterceptor响应:
"parents/654/profile/images/1530979485020.jpg"
是的!
我的日志:
"parents"
这是什么?
翻新有问题吗?
我也尝试替换
Call<String>
至Call<ResponseBody>
,但:
response.body.string() == null
我想念什么?