2016-08-17 87 views
0

大家好。Null Object In Retrofit2

我试图用retrofit2连接到一个RestApi,但这曾经给我错误或返回对象null。

{"news": [ { "News": { "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" } }]} 

我有下面的代码:

接口休息

@GET("news") 
Call<news> getNews(); 

POJO对象:

public class news { 

@SerializedName("News") 
public List<Listnews> listnews; 

public static class Listnews{ 

    public News news; 

    public static class News{ 

     @SerializedName("id") 
     private String id; 

     @SerializedName("title") 
     private String title; 

     @SerializedName("description") 
     private String description; 

     // getters and setters 
    } 

} 

,并显示在MainActivity

Call<news> call2 = api.getNews(); 

    call2.enqueue(new Callback<news>() { 

     @Override 
     public void onResponse(Call<news> call, Response<news> response) { 

      if (response.code() == 200){ 

       List<news.Listnews> respuesta = response.body().listnews; 

       System.out.println("title: "+ respuesta.get(0).news); 


      } 

     } 

     @Override 
     public void onFailure(Call<news> call, Throwable t) { 

      System.out.println(t + " Error "); 

     } 

    }); 

但是这个永远返回null。

回答

2

请不要在跟随着g改变你的课堂,希望这应该起作用。让我知道如果它不。

public class news { 

// Change 1 --- 
@SerializedName("news") 
public List<Listnews> listnews; 

public static class Listnews{ 

// Change 2 --- 
@SerializedName("News") 
public News news; 

public static class News{ 

    @SerializedName("id") 
    private String id; 

    @SerializedName("title") 
    private String title; 

    @SerializedName("description") 
    private String description; 

    // getters and setters 
} } 
0

在json字符串中有两个“新闻”,“新闻”。

由于模型注释具有下列

Ñ EWS(阵列)

- > ID

- >标题

- >描述

{ 
    "News": [ 
     { "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" }, 
     { "id": "2", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" } 
    ] 
} 

但在你的json字符串

{ 
    "news": [{ 
     "News": { "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" } 
    }] 
} 

你可以看到结构:

消息(阵列)

- >新闻(对象)

- > ID

- >标题

- >描述

因此,您不能对其中的任何数据进行阶段化。

以上就是你得到空对象的原因,你应该使用的第一个JSON结构得到的消息数据

以下是为您的工作

  1. 不要混淆与建议单词“新闻”和数据结构“新闻”(http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api)您可以使用“新闻”作为获取新闻列表的路径。但是如果你在返回JSON中有另一个“新闻”或“新闻报道”会很痛苦,因为它很难弄清楚哪一个应该是新闻。试试像“数据”,“项目”或只是返回新闻的数组列表,因为你的路径(http://restful.com/news)通知你正试图获取新闻列表

  2. 建立你的宁静的测试用例。如果您尝试在您的移动应用程序中测试每个宁静的API,将会很痛苦。尝试使用以下

    A.构建仪器测试(https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html)为您演示按钮点击使用改装(https://github.com/square/okhttp/tree/master/mockwebserver) 触发宁静

    B.模拟请求和你的JSON字符串复制到转义字符一个(使用本网站http://bernhardhaeussner.de/odd/json-escape/

这两种方式将节省大量的时间在发展

0

这对我有效。我不知道你为什么选择静态课。我已经做了简单的像这样:

POJO类News.java:

public class News 
{ 
String id, title, description; 

public String getId() 
{ 
    return id; 
} 

public void setId(String id) 
{ 
    this.id = id; 
} 

public String getTitle() 
{ 
    return title; 
} 

public void setTitle(String title) 
{ 
    this.title = title; 
} 

public String getDescription() 
{ 
    return description; 
} 

public void setDescription(String description) 
{ 
    this.description = description; 
} 
} 

API接口:

public interface NewsAPI 
{ 
    @POST("/loltry.php") 
    Call<NewsList> getNews(); 
} 

NewsList.java:

public class NewsList 
{ 
    @SerializedName("news") 
    ArrayList<Data> d; 
} 

Data.java:

public class Data 
{ 
    @SerializedName("data") 
    News n; 
} 

NewsList newsList = response.body(); 

String rootUrl = "http://192.168.0.5"; 
Retrofit adapter = new Retrofit.Builder().baseUrl(rootUrl).addConverterFactory(GsonConverterFactory.create()).build(); 
NewsAPI newsAPI = adapter.create(NewsAPI.class); 
final Call<NewsList> call = newsAPI.getNews(); 
call.enqueue(new Callback<NewsList>() 
{ 
    @Override 
       public void onResponse(Call<NewsList> call, Response<NewsList> response) 
       { 
        Toast.makeText(MainActivity.this, "Succcessss", Toast.LENGTH_LONG).show(); 
        newsList = response.body(); 
        try 
        { 
         for (int i = 0; i < newsList.d.size(); ++i) 
         { 
           newsListArray.add(i, newsList.d.get(i).n.getTitle()); 
         } 
        } catch (Exception e) 
        { 
          e.printStackTrace(); 
        } 
       } 

这里我的本地主机上的loltry.php正在发送你试图解析的json对象。我在列表视图中显示标题在“新闻”(我的“数据”)对象中。

这是我JSONResponse:

{"news":[{"data":{"id":"1","title":"Big data is changing the ace f fashion","description":"Download Fashioning Data: A 2015 Update"}}]} 
相关问题