这是我发生错误的api调用
public void loadData(){
pd=new ProgressDialog(this);
pd.setMessage("loading");
pd.show();
mApi= new RetrofitHelper<AuthApi>().getApi(AuthApi.class);
mCall=mApi.studentlist(pageNo);
mCall.enqueue(new Callback<ResultObject<ArrayList<Person>>>() {
@Override
public void onResponse(Call<ResultObject<ArrayList<Person>>> call, Response<ResultObject<ArrayList<Person>>> response) {
for(int i=0;i<response.body().getData().size();i++){
mData.add(response.body().getData().get(i));
}
count=response.body().getCount();
mAdapter=new PersonAdapter(Members.this,R.layout.item_person,mData);
list.setAdapter(mAdapter);
pd.hide();
}
@Override
public void onFailure(Call<ResultObject<ArrayList<Person>>> call, Throwable t) {
Toast.makeText(Members.this,"failed",Toast.LENGTH_SHORT).show();
pd.hide();
}
});
}
这是我的logcat,同时关闭api,因为你可以看到我正确得到了响应,我在后端跟踪球,没有错误就可以了
07-01 12:23:56.180 12981-13143/com.example.kethan.project D/OkHttp: --> GET http://192.168.0.107:8000/students/?page=1 http/1.1
07-01 12:23:56.180 12981-13143/com.example.kethan.project D/OkHttp: --> END GET
07-01 12:23:56.183 12981-12981/com.example.kethan.project D/ActivityThreadInjector: clearCachedDrawables.
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: <-- 200 OK http://192.168.0.107:8000/students/?page=1 (80ms)
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Date: Sat, 01 Jul 2017 06:53:53 GMT
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Server: WSGIServer/0.2 CPython/3.5.2
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Content-Length: 849
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Vary: Accept, Cookie
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: Content-Type: application/json
07-01 12:23:56.261 12981-13143/com.example.kethan.project D/OkHttp: X-Frame-Options: SAMEORIGIN
07-01 12:23:56.262 12981-13143/com.example.kethan.project D/OkHttp: Allow: GET, POST, HEAD, OPTIONS
07-01 12:23:56.263 12981-13143/com.example.kethan.project D/OkHttp: {"count":37,"next":"http://192.168.0.107:8000/students/?page=2","previous":null,"results":[{"id":35,"name":"kethan","mobile":"8142142827","time":"10:5:0","late":"late by: 5mins 0secs","date":"2017/6/16"},{"id":36,"name":"kethan","mobile":"8142142827","time":"23:28:0","late":"late by: 13hrs 28mins 0secs","date":"2017/06/29"},{"id":37,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":38,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":39,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":40,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":41,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"},{"id":42,"name":"kethan","mobile":"8142142827","time":"134","late":"123","date":"123"}]}
07-01 12:23:56.263 12981-13143/com.example.kethan.project D/OkHttp: <-- END HTTP (849-byte body)
07-01 12:23:56.292 12981-12981/com.example.kethan.project D/AndroidRuntime: Shutting down VM
--------- beginning of crash
07-01 12:23:56.292 12981-12981/com.example.kethan.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kethan.project, PID: 12981
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.example.kethan.project.Members$2.onResponse(Members.java:108)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
这是结果对象
public class ResultObject<T> {
private int count;
private T data;
public int getCount() {
return count;
}
public T getData() {
return data;
}
public void setCount(int count) {
this.count = count;
}
public void setData(T data) {
this.data = data;
}
}
这是Person pojo类
public class Person {
private int id;
private String time;
private String late;
private String date;
private String name;
private String mobile;
public String getDate() {
return date;
}
public int getId() {
return id;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getLate() {
return late;
}
public void setLate(String late) {
this.late = late;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void setDate(String date) {
this.date = date;
}
public void setId(int id) {
this.id = id;
}
}
答案 0 :(得分:2)
这意味着response.body().getData()
返回null。由于您无法获得null的大小,因此会抛出异常。
为了使问题更加清晰,让我们将循环分解为多行。 (这也是一个好主意,因此您的代码将更有效。您只需要一次调用size()
一次,而不是每次循环调用一次。
ArrayList<Person> listOfPersons = response.body().getData(); //This will be null, I suspect.
int size = listOfPersons.size(); //Exception will happen on this line, since listOfPersons is null
for(int i = 0; i < size; ++i)
{
mData.add(listOfPersons.get(i));
}
您需要执行安全检查,以验证ResultObject
是否包含您要查找的信息。点击断点并查看响应正文的内容。
答案 1 :(得分:0)
在继续使用它之前,您必须始终检查response.body()是否为null,因为服务器可能会向您发送错误,该错误将发送到response.error()。 如果你确定response.body()!= null然后检查你的pojo模型,可能会有错误。特别是在字段名称中,它必须与普通的json响应相同。
答案 2 :(得分:0)
str = "message[0][generic][0][elements][0][default_action][url]";
res = str.match(/\[([^\d\]]+)\](?=\[[^\d\]]*\]|$)/g)
.map(function(s) { return s.replace(/[\[\]]/g, "") })
.reverse();
console.log(res);