不使用Retrofit 2从服务器接收数据

时间:2017-02-24 20:04:40

标签: android json retrofit2

我正在尝试使用Retrofit 2从特定的subreddit中检索Reddit信息。我已经关注了许多教程和视频,从我的角度来看,我的代码似乎是正确的,但我只能在我的模型类中设置null对象。我在Manifest中获得了互联网许可。

这是我使用HERE

的JSON链接

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)
        {

        }
    });
  }
}

Api /服务类

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;
    }
}

public class Child
{
    private Data_ data;

    public Data_ getData()
    {
        return data;
    }

    public void setData(Data_ data)
    {
        this.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;
    }


}

2 个答案:

答案 0 :(得分:0)

您需要在mData = response.body()中添加onResponse()(同时先检查response.isSuccessful()

答案 1 :(得分:0)

问题是您的数据与Reddit JSON不对应。您的数据

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

与给定的json不匹配,即:

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

Retrofit自动从json转换为java,但前提是映射是正确的。 一个正确的Java类将是:

public class Subreddit {
  String kind;
  Data data;
}

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

然后将desks方法接口修改为

Call<Subreddit> desks();

您必须以递归方式获取JSON的整个深度才能获得正确的映射。 但在你开始工作之前,只需更换你的Retrofit界面:

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类