2017-02-24 81 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類

相關問題