post - Retrofit 2.0 无法将请求正文转换为 JSON

标签 post request gson retrofit

希望有人能帮助我。

我尝试使用 Retrofit 2.0 发送带有 JSON 正文的 POST 请求。

界面:

public interface Interface {
  @POST(/*path*/)
  Call<MyResponseObject> sendInt(@Body MyInteger myInt);
}

MyInteger 类:

public class MyInteger {
    int id;

    public MyInteger(int id) {
        this.id = id;
    }
}

主要事件的一部分:

private Retrofit mRetrofit = null;
private final Interface mService;
...
...
mRetrofit = new Retrofit.Builder()
                .baseUrl(/*URL*/)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
mService = mRetrofit.create(Interface.class);

通话:

MyInteger id = new MyInteger(0);
mService.sendInt(id).enqueue(new Callback<MyResponseObject>() {
  @Override
  public void onResponse(Call<MyResponseObject> call, Response<MyResponseObject> response) {/*Log something*/}

  @Override
  public void onFailure(Call<MyResponseObject> call, Throwable t) {}
});

在我看来,就像这个例子: https://futurestud.io/blog/retrofit-send-objects-in-request-body

但是 GsonConverter 无法将 MyInteger 转换为 JSON.. 这是日志:

    java.lang.IllegalArgumentException: Unable to create @Body converter for class com.??.MyInteger (parameter #1)
                                                                                                   for method Interface.sendInt
...
...
Caused by: java.lang.IllegalArgumentException: Could not locate RequestBody converter for class com.??.MyInteger.
                                                                                             Tried:
                                                                                              * retrofit2.BuiltInConverters
                                                                                              * retrofit2.GsonConverterFactory
                                                                                               at retrofit2.Retrofit.nextRequestBodyConverter(Retrofit.java:288)
                                                                                               at retrofit2.Retrofit.requestBodyConverter(Retrofit.java:248)
                                                                                               at retrofit2.RequestFactoryParser.parseParameters(RequestFactoryParser.java:491)

最佳答案

我也遇到了同样的问题。根本原因是我使用了不兼容的库。

这个组合对我有用:

    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>retrofit</artifactId>
        <version>2.0.0-beta4</version>
    </dependency>
    <dependency>
    <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>converter-gson</artifactId>
        <version>2.0.0-beta4</version>
    </dependency>

关于post - Retrofit 2.0 无法将请求正文转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35768656/

相关文章:

android - java.lang.IllegalArgumentException : Parameter specified as non-null is null while parsing JSON to kotlin Data class 异常

php - slim框架中如何处理POST请求?

javascript - IE10/IE11 清除缓存后中止 Post Ajax 请求,错误为 "Network Error 0x2ef3"

java - 是否可以从 REST 请求中获取 POST 参数?

java - 如何获取上一个URL页面?引荐来源网址不起作用

java - 内部类的Gson到pojo序列化

php - 使用 PHP 发布并获取同一页面的表单

node.js - 在 Node.js 中使用 REST API 时请求参数

python - 请求 Python : Passing payload POST request

java - 用Java解析JSON数组