java - retorfit2 如何让这个 json 正确?

标签 java android json gson retrofit2

我的json字符串生成如下:

    header('Content-type:text/json');
    if (mysql_num_rows ( $result ) != 0) {
        $row = mysql_fetch_assoc($result);
        $jobj=new stdclass();
        foreach($row as $key=>$value){
            $jobj->$key=$value;
        }
    }
    echo json_encode($jobj,JSON_NUMERIC_CHECK);

在浏览器中,结果是 {"user_id":1,"step":1,"status":1}

我的javabean是:

public class Login {
    private int user_id;
    private int status;
    private int step;

    public Login(int user_id, int status, int step) {
        this.user_id = user_id;
        this.status = status;
        this.step = step;
    }
}

设置...获取...等

当我使用 retrofit2 时:

instance = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("http://192.168.1.108/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

我明白了

android.content.res.Resources$NotFoundException: String resource ID #0x1

当我将 JavaBean 更改为:

public class Login {
    private String user_id;
    private String status;
    private String step;


}

我得到了正确的结果。但我想要一个数字,而不是字符串。怎么做?

最佳答案

问题不在于 Retrofit 或 JSON。

当您尝试将文本设置为 TextView 时发生 Resources$NotFoundException。

如果将整数提供给 TextView 的方法 setText,它会查找字符串资源,如果找不到则崩溃。

因此,您应该先将 Integer 转换为 String,然后再将其设置到 TextView 中。

使用这样的东西:

textView.setText(String.valueOf(login.user_id));

关于java - retorfit2 如何让这个 json 正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53662998/

相关文章:

android - 与其他帐户共享 Android Market 统计信息

javascript - 带 child 的 JSON 解析器

java google Drive API无法下载二进制文件

java - 使用 Cucumber 中的 AssertJ Swing 测试

android - 抽屉导航点击未触发

java - 在 Java 中将 XML 转换为 json,同时保留数据类型

javascript - 最不同的数据交换格式?

java - 如果源包含 Elastic Search Server 中给定的搜索文本,则获取所有文档

java - 启用 JMX 代理是否有性能开销?

android - 无法以编程方式访问 Action Bar 项目 View - Android