我在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
答案 0 :(得分:2)
请使用jsonschema2pojo网站生成模型 的 StudentData 强>:
angular.module('thisApp', [someDependencies]).config( function($stateProvider, $urlRouteProvider) {
$http.get('url').then(function(response) {
var alpha = response.data; // i need to access this data in the controller
)};
}).directive('pqrTitle', function () {
return {
restrict: 'A',
controller: function($scope, $rootScope) {
// I need to access alpha here
});
});
<强>学生强>:
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;
}
}
答案 1 :(得分:0)
修改强> 问题出在StudentData类中。 而不是学生的实例,你必须像这样创建类:
public static class StudentData {
List<Student> student;
public int success;
...
}
JSON清楚地表明带有“student”键的元素是一个Student实例数组。