gpt4 book ai didi

android - 使用 Retrofit 库获取时出错

转载 作者:行者123 更新时间:2023-11-29 20:37:27 25 4
gpt4 key购买 nike

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

最佳答案

请使用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;
}

}

关于android - 使用 Retrofit 库获取时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31335949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com