使用 Retrofit 库获取时出错

Error While Fetching with Retrofit Library

我在 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

中创建了一个静态 class
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 class

public interface WeatherAPI {

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

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

}

不知道这个有什么问题returns改造错误函数

改装错误是

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

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

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

JSON 清楚地表明带有 "student" 键的元素是一个 Student 实例数组。

请使用 jsonschema2pojo 网站生成您的模型。
学生数据:

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;
}

}