Android 使用改装库获取时出错
我在MainActivity中创建了一个函数,按下刷新按钮时调用该函数Android 使用改装库获取时出错,android,json,retrofit,Android,Json,Retrofit,我在MainActivity中创建了一个函数,按下刷新按钮时调用该函数 public void callWeatherAPI(){ SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); final String strUserName = SP.getString("username", "NA"); int default
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();
}
});
}
}
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);
编辑:
问题出在StudentData类中。
您必须创建这样的类,而不是Student实例:
public static class StudentData {
List<Student> student;
public int success;
...
}
公共静态类StudentData{
列出学生名单;
公众的成功;
...
}
JSON清楚地表明,带有“student”键的元素是一个student实例数组。请使用site生成您的模型。学生数据:
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;
}
}
{“成功”:1,“学生”:[{“考试卷”:“1212”,“班级号”:“121”,“班级号”:“1221”,“姓名”:“rohit”}}}里面有一个数组!我知道,我看到了JSON。但可能原始JSON的结构与此不同。你试过列表回调吗?我试过上面发布的代码我得到了json成功的结果现在我正在努力如何访问数组对象,即考试卷,课程号。哦,现在我知道问题出在哪里了…看看我的答案的编辑好的,我已经按照你在本课程中说的那样成功了(List studentDatas,Response Response){}如何访问json数据?我还没有使用List much您能告诉我如何检索name public void success(final StudentData StudentData,Response arg1){Log.d(“Debug”,StudentData.student.get(0));}我得到空点,除了
Log.d(“调试”,studentData.student.get(0));
,然后执行以下操作:Log.d(“调试”,studentData);
并显示结果它不适用!Log.d需要string@ShresthaRohit,Log.d(“调试”,studentData+”);d/Debug﹕ ku.example.com.seatplanku.main活动$StudentData@b2fc3010
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;
}
}