@Multipart
@POST("http://ec2-18-216-193-78.us-east-
2.compute.amazonaws.com:3000//api/v1/snapNShare")
Call<SnapNShareResponse> sendUserReview(
@Header("Authoriozation") String token,
@Part("restaurantInfoId") String restaurantInfoId,
@Part MultipartBody.Part dishPicture,
@Part MultipartBody.Part audioReview,
@Part("textReview") String textReview,
@Part("rating") Integer rating);
ApiClient如下:在这里,我的项目与Dagger库集成 .
@Singleton
public class ApiClient {
@Inject
ApiClient() {
}
private static Retrofit retrofitBaseURL = null;
private static Retrofit retrofitGOOGLEURL = null;
public static Retrofit getClient(Context context, String base_url) {
if (NetworkUtility.isNetworkAvailable(context)) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
.readTimeout(180, TimeUnit.SECONDS)
.connectTimeout(180, TimeUnit.SECONDS)
.build();
if (null == retrofitBaseURL && base_url.equals(WebConstants.BASE_URL)) {
retrofitBaseURL = new Retrofit.Builder()
.baseUrl(base_url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
} else if (null == retrofitGOOGLEURL && base_url.equals(WebConstants.GOOGLE_BASE_URL)) {
retrofitGOOGLEURL = new Retrofit.Builder()
.baseUrl(base_url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return base_url.equals(WebConstants.BASE_URL) ? retrofitBaseURL : retrofitGOOGLEURL;
} else {
SnapXToast.showLongToast(context, "");
return null;
}
}
}
这是我的改装请求,其中我想上传图像和音频文件到aws服务器 . 而改造功能如下:
public void sendReview(String restId, Uri image, Uri audio, String txtReview, Integer rating) {
if (NetworkUtility.isNetworkAvailable(mContext)) {
ApiHelper apiHelper = ApiClient.getClient(mContext, BASE_URL).create(ApiHelper.class);
String fileImagePath = getRealPathFromURIPath(image, mContext);
String fileAudioPath = getRealPathFromURIPath(audio, mContext);
File fileImg = new File(fileImagePath);
File fileAud = new File(fileAudioPath);
RequestBody mFileImage = RequestBody.create(MediaType.parse("*/*"), fileImg);//here I tried with media type "image/*"
RequestBody mFileAudio = RequestBody.create(MediaType.parse("*/*"), fileAud);//here I tried with media type "audio/*"
MultipartBody.Part imageUpload = MultipartBody.Part.createFormData("dishPicture", fileImg.getName(), mFileImage);
MultipartBody.Part audioUpload = MultipartBody.Part.createFormData("audioReview", fileAud.getName(), mFileAudio);
Call<SnapNShareResponse> snapXUserCall = apiHelper.sendUserReview(
utility.getAuthToken(mContext)
, restId, imageUpload, audioUpload, txtReview, rating);
snapXUserCall.enqueue(new Callback<SnapNShareResponse>() {
@Override
public void onResponse(Call<SnapNShareResponse> call,
Response<SnapNShareResponse> response) {
if (response.isSuccessful() && null != response.body()) {
SnapNShareResponse nShareResponse = response.body();
mReviewPresenter.response(SnapXResult.SUCCESS, nShareResponse);
SnapXToast.showToast(mContext,"yasss");
}
}
@Override
public void onFailure(Call<SnapNShareResponse> call, Throwable t) {
mReviewPresenter.response(SnapXResult.FAILURE, null);
}
});
} else {
mReviewPresenter.response(SnapXResult.NONETWORK, null);
}
}
//响应得到的是,
403 Forbidden http://ec2-18-216-193-78.us-east-2.compute.amazonaws.com:3000/api/v1/snapNShare(16878ms)X-Powered-By:Express Access-Control-Allow-Origin:* 04-29 22:20:29.685 9724-14733 / com.snapxeats D / OkHttp:Content-Type:application / json; charset = utf-8 Content-Length:36 ETag:W / "24-9ze4gN/8B8Z0boWkFyxw9xuo9cA" Date:Sun,29 Apr 2018 16:50:30 GMT Connection:keep-alive 04-29 22:20:29.690 9724-14733 / com.snapxeats D / OkHttp:{"message":"User not authenticated"}
我是否需要在我的请求中单独添加“ Headers ”部分?
另外在logcat的图像和音频我得到这样的日志:
J p?p AN ynnd ʖ V g 8C# 凨4 h 7qL-߾〜 h41BF 4 & T \ yqAIRj @НJ)2RAo =β.-HH $ NGX {PռQM < 5 l sI q --cd1c8557-c8c3-498f-80ac-3434f9d19840 Content-Disposition:form-data; name = "audioReview"; filename = "AUD_20180429_213143.mp3" Content-Type:/ Content-Length:4603
//对于图像来说相同
对于“意外的EOF”,我添加了“mPlayer.release();”这消除了错误 . 任何人都可以帮我解决我失踪的部分吗?我检查了文件名的图像和音频;它们已成功保存到本地 . 并使用://改造实现'com.squareup.retrofit2:改造:2.2.0'实现'com.squareup.retrofit2:converter-gson:2.1.0'
如果我需要添加有关查询的更多信息,请现在就让我 . 谢谢 !!