【问题标题】:Error While Fetching with Retrofit Library使用改造库获取时出错
【发布时间】:2015-07-10 08:27:46
【问题描述】:

我在 MainActivity 中创建了在按下刷新按钮时调用的函数

public void callWeatherAPI(){

        SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        final String strUserName = SP.getString("username", "NA");
        int defaultval = Integer.parseInt(strUserName);




        WeatherAPI weatherAPI = APIHandler.getApiInterface();
        weatherAPI.getWeatherCity(defaultval, new Callback<StudentData>() {

            public void success(final StudentData arg0, Response arg1) {

                Toast.makeText(getApplicationContext(), arg0.getStudentName(), Toast.LENGTH_LONG).show();
               // Toast.makeText(getApplicationContext(), WeatherData.getWeatherCity().getDescription(), Toast.LENGTH_LONG).show();

            }

            public void failure(RetrofitError arg0) {

                Toast.makeText(getApplicationContext(), "OPS, some kind of problem had happend! :(", Toast.LENGTH_LONG).show();
            }
        });
    }
}

并在 MainActivity 中创建了一个静态类

public static class Student{
    public String exam_roll;
    public String class_no;
    public String block_no;
    public String name;

}
public static class StudentData{
    Student student;
    public int success;


    public int getStudentName() {
        return success;
    }
}

json是谁的

{"success":1,"student":[{"exam_roll":"1212","class_no":"121","block_no":"1221","name":"rohit"}]}    

我已将回调函数设置为指向 StudentData 类

public interface WeatherAPI {

//@GET("/weather")
@GET("/")

void getWeatherCity(@Query("pid") int pid, Callback<MainActivity.StudentData> callback);

}

不知道这是什么问题,它返回改造错误函数

改造的错误是

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 25

【问题讨论】:

    标签: android json retrofit


    【解决方案1】:

    请使用jsonschema2pojo 网站生成您的模型。
    StudentData

    public class StudentData {
    
    @Expose
    private Integer success;
    @Expose
    private List<Student> student = new ArrayList<Student>();
    
    /**
    * 
    * @return
    * The success
    */
    public Integer getSuccess() {
    return success;
    }
    
    /**
    * 
    * @param success
    * The success
    */
    public void setSuccess(Integer success) {
    this.success = success;
    }
    
    /**
    * 
    * @return
    * The student
    */
    public List<Student> getStudent() {
    return student;
    }
    
    /**
    * 
    * @param student
    * The student
    */
    public void setStudent(List<Student> student) {
    this.student = student;
    }
    
    }
    

    学生

    public class Student {
    
    @SerializedName("exam_roll")
    @Expose
    private String examRoll;
    @SerializedName("class_no")
    @Expose
    private String classNo;
    @SerializedName("block_no")
    @Expose
    private String blockNo;
    @Expose
    private String name;
    
    /**
    * 
    * @return
    * The examRoll
    */
    public String getExamRoll() {
    return examRoll;
    }
    
    /**
    * 
    * @param examRoll
    * The exam_roll
    */
    public void setExamRoll(String examRoll) {
    this.examRoll = examRoll;
    }
    
    /**
    * 
    * @return
    * The classNo
    */
    public String getClassNo() {
    return classNo;
    }
    
    /**
    * 
    * @param classNo
    * The class_no
    */
    public void setClassNo(String classNo) {
    this.classNo = classNo;
    }
    
    /**
    * 
    * @return
    * The blockNo
    */
    public String getBlockNo() {
    return blockNo;
    }
    
    /**
    * 
    * @param blockNo
    * The block_no
    */
    public void setBlockNo(String blockNo) {
    this.blockNo = blockNo;
    }
    
    /**
    * 
    * @return
    * The name
    */
    public String getName() {
    return name;
    }
    
    /**
    * 
    * @param name
    * The name
    */
    public void setName(String name) {
    this.name = name;
    }
    
    }
    

    【讨论】:

    • 你能告诉我如何检索名称 public void success(final StudentData studentData, Response arg1) { Log.d("Debug",studentData.student.get(0)); } 我得到空点异常
    • 代替Log.d("Debug",studentData.student.get(0));,执行下一步:Log.d("Debug",studentData); 并显示结果
    • 不适用! Log.d 需要字符串
    • @ShresthaRohit, Log.d("Debug",studentData + "");
    • D/Debug:ku.example.com.seatplanku.MainActivity$StudentData@b2fc3010
    【解决方案2】:

    编辑: 问题出在您的 StudentData 类中。 您必须像这样创建类,而不是 Student 的实例:

    public static class StudentData { 
        List<Student> student;
        public int success;
        ...
    } 
    

    JSON 清楚地表明,带有“student”键的元素是一组 Student 实例。

    【讨论】:

    • {"success":1,"student":[{"exam_roll":"1212","class_no":"121","block_no":"1221","name":" rohit"}]} 里面有一个数组!
    • 我知道,我看到了 JSON。但也许原始 JSON 的结构与此不同。您尝试过 List 回调吗?
    • 我尝试使用我在上面发布的相同代码我得到了 json 成功的结果现在我正在努力如何访问数组对象,即exam_roll,class_no。
    • 哦,现在我知道问题出在哪里了……看看我的答案的编辑
    • 好吧,我按照你在本次会议中所说的做了 public void success(List studentDatas, Response response) { } 我如何访问 json 数据?我没怎么用过List
    猜你喜欢
    • 2016-02-10
    • 2020-07-20
    • 2017-05-27
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2016-08-22
    • 2018-04-20
    • 2015-08-20
    相关资源
    最近更新 更多