-1
即時消息在這裏,我有一個問題,我無法解決。Android JsonDeserializer在返回響應時出錯
即時得到這個錯誤在調試中執行時間:
05-02 11:56:07.539 18645-24466/com.appss.appssapp D/Retrofit﹕ java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0, v2}, java.util.ArrayList com.appss.appssapp.io.model.PostsResponse$MainResponse.access$002(com.appss.appssapp.io.model.PostsResponse$MainResponse, java.util.ArrayList) // [email protected]'
at com.appss.appssapp.io.model.PostsResponse.setPosts(PostsResponse.java:21)
at com.appss.appssapp.io.deserializer.PostsResponseDeserializer.deserialize(PostsResponseDeserializer.java:31)
at com.appss.appssapp.io.deserializer.PostsResponseDeserializer.deserialize(PostsResponseDeserializer.java:21)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:818)
應用程序被編譯正確,沒有問題,運行,但是當我向API調用不顯示數據,我意思是,api發送數據,我做了我必須做的事情,但是當我嘗試發送de數據到調試中的響應時,顯示出我上面寫到的問題。
這是我的代碼。
Posts.JAVA
public class Posts {
String id; //Id de la publicacion
String titulo; // Titulo de la publicacion
String url; // Url de la publicacion
String imagen; // Imagen cover la publicacion
String autor_nick; // Nick del autor de la publicacion
String visitas; // Lecturas de la publicacion
String comentarios; // Cantidad de comentarios de la publicacion
public Posts() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getImagen() {
return imagen;
}
public void setImagen(String imagen) {
this.imagen = imagen;
}
public String getAutor_nick() {
return autor_nick;
}
public void setAutor_nick(String autor_nick) {
this.autor_nick = autor_nick;
}
public String getVisitas() {
return visitas;
}
public void setVisitas(String visitas) {
this.visitas = visitas;
}
public String getComentarios() {
return comentarios;
}
public void setComentarios(String comentarios) {
this.comentarios = comentarios;
}
}
PostsResponse.JAVA
public class PostsResponse {
private MainResponse mainResponse;
public ArrayList<Posts> getPosts(){
return mainResponse.posts;
}
public void setPosts(ArrayList<Posts> posts) {
mainResponse.posts = posts;
}
private class MainResponse {
private ArrayList<Posts> posts;
}
}
PostResponseDeserializer.JAVA
public class PostsResponseDeserializer implements JsonDeserializer<PostsResponse> {
@Override
public PostsResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Gson gson = new Gson();
PostsResponse response = gson.fromJson(json, PostsResponse.class);
JsonObject postsResponseData = json.getAsJsonObject();
JsonArray postsArray = postsResponseData.getAsJsonArray("posts");
response.setPosts(extractPostsFromJsonArray(postsArray));
return response;
}
private ArrayList<Posts> extractPostsFromJsonArray(JsonArray postsArray) {
ArrayList<Posts> posts = new ArrayList<>();
for (int i = 0; i < postsArray.size(); i++) {
JsonObject postsDatos = postsArray.get(i).getAsJsonObject();
Posts actualPost = new Posts();
String titulo = postsDatos.get("titulo").getAsString();
String imagen = postsDatos.get("imagen_370x208").getAsString();
String visitas = postsDatos.get("visitas").getAsString();
actualPost.setTitulo(titulo);
actualPost.setImagen(imagen);
actualPost.setVisitas(visitas);
posts.add(actualPost);
}
return posts;
}
}
我認爲問題可能在這裏 response.setPosts(extractPostsFromJsonArray(postsArray));
但我不明白爲什麼會發生這種情況。
任何幫助? ..對我的壞英語感到抱歉。
我是Android和Java新手。你給我的答案解決了我的問題。非常感謝! –