【问题标题】:"Cannot resolve method 'fromJson'“无法解析方法'fromJson'
【发布时间】:2019-10-10 22:34:21
【问题描述】:

我试图读取 Json 文件的信息,但出现错误提示:

Cannot resolve method 'fromJson(android.util.JsonReader, java.lang.Class<com.proyecto.matriculas.model.Matricula>)'

我有其他类,我在其中获取文件信息以及与服务器进行连接的位置,其中是 json 文件,唯一的问题就在这里,在这个类中。

我尝试的课程是:

public class GsonMatriculaParser {
    public List leerFlujoJson(InputStream in) throws IOException {
        Gson gson = new Gson();
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        List<Matricula> matriculas = new ArrayList<>();

        reader.beginArray();

        while (reader.hasNext()) {
            Matricula matricula = (Matricula) gson.fromJson(reader, Matricula.class);
            matriculas.add(matricula);
        }


        reader.endArray();
        reader.close();
        return matriculas;
    }
}

错误在 gson.fromJson(reader, Matricula.class);

我在build.gradle 中使用implementation 'com.google.code.gson:gson:2.8.5' 作为我的gson 依赖项。

班级矩阵:

public class Matricula {

private Integer N_Registro;
    private String Infraccion;
    private String Fecha_Infraccion;
    private String N_Matricula;
    private Integer IDPropietariosFK;


    public Matricula(Integer N_Registro,  String Infraccion, String Fecha_Infraccion, String N_Matricula, Integer IDPropietariosFK) {
        this.N_Registro = N_Registro;
        this.Infraccion = Infraccion;
        this.Fecha_Infraccion = Fecha_Infraccion;
        this.N_Matricula = N_Matricula;
        this.IDPropietariosFK = IDPropietariosFK;
    }

    public Integer getN_Registro() {
        return N_Registro;
    }

    public void setN_Registro(Integer N_Registro) {
        this.N_Registro = N_Registro;
    }

    public String getInfraccion() {
        return Infraccion;
    }

    public void setInfraccion(String Infraccion) {
        this.Infraccion = Infraccion;
    }

    public String getFecha_Infraccion() {
        return Fecha_Infraccion;
    }

    public void setFecha_Infraccion(String Fecha_Infraccion) { this.Fecha_Infraccion = Fecha_Infraccion; }

    public String getN_Matricula() {
        return N_Matricula;
    }

    public void setN_Matricula(String N_Matricula) {
        this.N_Matricula = N_Matricula;
    }

    public Integer getIDPropietariosFK() {
        return IDPropietariosFK;
    }

    public void setIDPropietariosFK(Integer IDPropietariosFK) { this.IDPropietariosFK = IDPropietariosFK; }

}

我不知道问题出在依赖关系上还是出在我的一个类的代码中...

【问题讨论】:

    标签: android gson google-play-services fromjson


    【解决方案1】:

    你用错了JsonReader:

    • 您正在使用android.util.JsonReader
    • 参数为com.google.gson.stream.JsonReader

    【讨论】:

    • 是的,我导入错误...谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 2021-01-18
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多