2017-02-24 80 views
0

我想从一个特定的subreddit使用Retrofit 2检索Reddit信息。我遵循了很多教程和视频和我的代码似乎是正确的从我的角度来看,但我只能设法null我的模型类中的对象。我有清单中的互联网许可。没有从服务器接收数据使用改进2

这是一个链接,我与HERE

MainActivity

public class MainActivity extends AppCompatActivity 
{ 
    TextView mTextView; 
    Data mData; 
    private static final String TAG = "Battlestations"; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    mTextView = (TextView) findViewById(R.id.test_view); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Call<Data> serviceCall = Service.getDesktopService().desks(); 
    serviceCall.enqueue(new Callback<Data>() 
    { 
     @Override 
     public void onResponse(Call<Data> call, Response<Data> response) 
     { 

      Log.d("Reponce","return"); 
      Log.i(TAG, "Response is " + mData.getChildren()); 
     } 

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

     } 
    }); 
    } 
} 

阿比/服务类

public class Service 
{ 
    private static final String BASE_URL = "https://www.reddit.com/r/"; 
    private static DeskInterface mRetrofit; 


    public static DeskInterface getDesktopService() 
    { 
     if(mRetrofit == null) 
     { 
      Retrofit build = new Retrofit.Builder() 
        .baseUrl(BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 

      mRetrofit = build.create(DeskInterface.class); 
     } 
     return mRetrofit; 
    } 

    public interface DeskInterface 
    { 
     @GET("battlestations/hot/.json") 
     Call<Data> desks(); 
    } 

} 

数据

public class Data 
{ 
    private List<Child> children = null; 

    public List<Child> getChildren() 
    { 
     return children; 
    } 

    public void setChildren(List<Child> children) 
    { 
     this.children = children; 
    } 
} 

儿童工作的JSON

public class Child 
{ 
    private Data_ data; 

    public Data_ getData() 
    { 
     return data; 
    } 

    public void setData(Data_ data) 
    { 
     this.data = data; 
    } 
} 

data_中

public class Data_ 
{ 
    private String subreddit; 
    private Integer score; 
    private String author; 
    private String subredditNamePrefixed; 
    private String url; 
    private String title; 

    public String getSubreddit() 
    { 
     return subreddit; 
    } 

    public void setSubreddit(String subreddit) 
    { 
     this.subreddit = subreddit; 
    } 

    public Integer getScore() 
    { 
     return score; 
    } 

    public void setScore(Integer score) 
    { 
     this.score = score; 
    } 

    public String getAuthor() 
    { 
     return author; 
    } 

    public void setAuthor(String author) 
    { 
     this.author = author; 
    } 

    public String getSubredditNamePrefixed() 
    { 
     return subredditNamePrefixed; 
    } 

    public void setSubredditNamePrefixed(String subredditNamePrefixed) 
    { 
     this.subredditNamePrefixed = subredditNamePrefixed; 
    } 

    public String getUrl() 
    { 
     return url; 
    } 

    public void setUrl(String url) 
    { 
     this.url = url; 
    } 

    public String getTitle() 
    { 
     return title; 
    } 

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


} 

回答

0

您需要添加onResponse()mData = response.body()(还要检查response.isSuccessful()第一)

0

的问题是,你的数据不会与reddit的JSON对应。你数据

public class Data { 
    private List<Child> children = null; 
} 

不匹配给定的JSON,那就是:

{ 
    "kind":"listing", 
    "data":{ 
    "modhash":"...", 
    "children":[...], 
    "after":"...", 
    "before":"..." 
    } 
} 

改造自动地从JSON转换为Java,但只有当映射是正确的。 正确的Java类是:

public class Subreddit { 
    String kind; 
    Data data; 
} 

public class Data{ 
    String modhash; 
    List<Child> children; 
    String after; 
    String before; 
} 

,然后修改书桌方法接口

Call<Subreddit> desks(); 

你将不得不递归去为JSON的整个深度得到正确的映射。 但你去上班前,只需更换您的更新接口:

public interface DeskInterface{ 
    @GET("battlestations/hot/.json") 
    Call<Data> desks(); 
} 

有:

public interface DeskInterface{ 
    @GET("battlestations/hot/.json") 
    Call<JsonObject> desks(); 
} 

,它应该返回的东西。如果仍然为空,则需要进一步调查。如果它返回一个有效的响应(一些json文本),然后复制/粘贴该subreddit到this website,在那里它将所有json转换为有效的Java类

相关问题