繁体   English   中英

我无法在真实设备中进行改造后发布,但它可以在模拟器中运行

[英]I can't post with retrofit in real device but it works in emulator

我有一个应用程序,发布带有一些字符串的文件。 我使用了改造 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");
                    }
                });
            }

我不确定这会解决每个人的问题,在我的情况下,它的一个文件已过期,导致服务器崩溃,因为它在模拟器上而不是在物理设备上工作,请执行以下操作:

  1. 让 Postman 在测试中使用它。
  2. 使用改造记录器拦截器收集日志并从物理设备和模拟器获取请求的所有值
  3. 在邮递员上尝试工作请求正文(在这种情况下是模拟器)
  4. 尝试将邮递员中工作请求主体(模拟器)的每个字段替换为来自不工作的请求主体(真实设备)的对应字段,并在每次更改时测试请求,直到其中一个导致错误。

在我的情况下,userId 已过期,这会导致服务器出错

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM