@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;
}
}
}
这是我的改装请求,我希望将上传图像和音频文件发送到服务器。改造功能如下:
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:快递 Access-Control-Allow-Origin:* 04-29 22:20:29.685 9724-14733 / com.snapxeats D / OkHttp:Content-Type:application / json;字符集= utf-8的 内容长度:36 ETag:W /“24-9ze4gN / 8B8Z0boWkFyxw9xuo9cA” 日期:太阳,2018年4月29日16:50:30 GMT 连接:保持活力 04-29 22:20:29.690 9724-14733 / com.snapxeats D / OkHttp:{“message”:“用户未经过身份验证”}
我是否需要在我的请求中单独添加“标题”部分?
另外在图像和音频的logcat中我得到了这样的日志:
太平绅士?pANynndʖVg8C#凨4h7qL-߾〜 h41BF 4 &amp; T \y q A I Rj @НJ )2 RAo=β.- hH $ nGx { P qM &lt; 5 l sI q --cd1c8557-c8c3-498f-80AC-3434f9d19840 内容处理:表格数据; NAME = “audioReview”;文件名= “AUD_20180429_213143.mp3” 内容类型: / 内容长度:4603
//与图像相同
对于“意外的EOF”我添加了“mPlayer.release();”这消除了错误。 任何人都可以帮我解决我失踪的部分吗?我检查了文件名的图像和音频;它们已成功保存到本地。并使用: //改造 实现'com.squareup.retrofit2:retrofit:2.2.0' 实现'com.squareup.retrofit2:converter-gson:2.1.0'
如果我需要添加有关查询的更多信息,请立即告诉我。 谢谢!!