【问题标题】:How do I call retrofit along with greendao for post request?我如何将改造与 greendao 一起用于发布请求?
【发布时间】:2019-04-24 19:13:39
【问题描述】:

我正在尝试将 greendao 与改造集成。此链接将提供有关如何将数据发送到服务器的想法 (https://i.stack.imgur.com/7qmbu.jpg)。这是一个发布请求,我真的很困惑如何通过改造来调用这个请求。

如果有人可以帮助我,那将非常有帮助。

在 API 响应中,我得到一个请求对象、响应对象、消息和状态代码。

响应对象我有关于用户的字段,在请求对象中我有关于正在发送的信息的字段。

这里有另一张照片 https://i.stack.imgur.com/a5DBz.jpg

【问题讨论】:

  • 您可以使用@Body 属性Retrofit POST JSON 对象
  • @AliAhmed 我已经这样做了,但仍然显示错误。请检查我添加了另一张图片

标签: android api android-studio retrofit greendao


【解决方案1】:

您可以使用此方法创建这样的响应

private String create(String email, String password) {
    try {
        JSONObject cell1 = new JSONObject();
        JSONObject jsonObject = new JSONObject();

        cell1.put("email", email);
        cell1.put("password", password);

        jsonObject.put("data", cell1);

        return jsonObject.toString();

    } catch (Exception e) {
        return e.getMessage();
    }
}

称你为 POST

@FormUrlEncoded 
@POST("your_path_here") 
Call<String> uploadData(@Body String obj); 

在您的活动中

String json = create("your_email", "your_password");

apiInterface.uploadData(json).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

【讨论】:

  • 它给出错误“@Body 参数不能与表单或多部分编码一起使用。(参数 #1)”。我现在该怎么办?
  • 问题已解决 - 我使用字段而不是使用正文来发送 json 对象并得到我的结果。感谢您的帮助。
猜你喜欢
  • 2014-08-18
  • 2020-08-21
  • 1970-01-01
  • 2019-03-27
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 2020-10-16
相关资源
最近更新 更多