我有一个应用程序,用一些字符串发布文件。我用过改造2.一切都好。但我有一个问题。我在模拟器中运行应用程序它工作正常并转到onResponse并发布文件和字符串,但是当我在真实设备中运行应用程序时它不起作用并且在改造中转到onFailure。问题是什么。我使用galaxy s6 edge和android 7 API 24.而模拟器是android 7.1.1 API 25.请帮助我解决这个问题:(
这是我的改造代码:
if (name.getText().toString().equals("") ||
description.getText().toString().equals("") ||
deadline.getText().toString().equals("")) {
popupForErrorEmptyField();
} else {
Client client = ServiceGenerator.createService(Client.class);
File file = null;
RequestBody requestFile = null;
MultipartBody.Part body = null;
if (userProfileImagePath != null) {
try {
file = new File(userProfileImagePath);
Uri uri = Uri.fromFile(file);
if (file.exists()) {
Log.i("finaltest", "found");
requestFile =
RequestBody.create(MediaType.parse(getMimeType(uri)), file);
body =
MultipartBody.Part.createFormData("projectFile", file.getName(), requestFile);
} else {
Log.i("finaltest", "not found");
}
} catch (Exception e) {
}
}
RequestBody nameRequestBody = RequestBody.create(MediaType.parse("text/plain"), name.getText().toString().trim());
RequestBody amountRequestBody = RequestBody.create(MediaType.parse("text/plain"), amount.getText().toString().trim());
RequestBody descriptionRequestBody = RequestBody.create(MediaType.parse("text/plain"), description.getText().toString().trim());
RequestBody categoryRequestBody = RequestBody.create(MediaType.parse("text/plain"), category.getSelectedItem().toString());
RequestBody deadlineRequestBody = RequestBody.create(MediaType.parse("text/plain"), deadline.getText().toString().trim());
Call<AddProjectResponse> call = client.addProject(
token,
body,
nameRequestBody,
amountRequestBody,
descriptionRequestBody,
categoryRequestBody,
deadlineRequestBody);
call.enqueue(new Callback<AddProjectResponse>() {
@Override
public void onResponse(Call<AddProjectResponse> call,
Response<AddProjectResponse> response) {
Log.e("xcxc", "code:" + response.code());
if (response.isSuccessful()) {
Log.e("xcxc", "success:");
popupForSuccessAddingProject();
} else {
Toast.makeText(getContext(), "successfull!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<AddProjectResponse> call, Throwable t) {
Log.e("test123", "error in add project");
}
});
}
答案 0 :(得分:0)
我不确定这是否可以解决每个人的问题,就我而言,这是一个已过期的文件,导致服务器崩溃,因为它在模拟器上而不是在物理设备上运行,请执行以下操作:
在我的情况下,userId已过期,这导致服务器出现错误