改造响应代码200但是空体

时间:2017-11-23 03:46:01

标签: retrofit

我试图从www.reddit.com/.json检索一个对象值但由于response.body()无法检测到,但错误代码为200,FYI json_feed和数据类在一个文件夹中另一个文件夹包含子项和数据类,请帮忙..

接口

public interface RedditAPI {

String Base_URL = "https://www.reddit.com/";

@Headers("Content-Type: application/json")
@GET(".json")
Call<json_feed> getData();}

主要活动

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Base_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    RedditAPI redditapi = retrofit.create(RedditAPI.class);
    Call<json_feed> call = redditapi.getData();

        call.enqueue(new Callback<json_feed>() {
            @Override
            public void onResponse(Call<json_feed> call, Response<json_feed> response) {

                ArrayList<Children> childrenList =  response.body().getData().getChildren();
                for (int i = 0; i < childrenList.size(); i++) {
                    Log.d("OnResponse Data = \n",
                            "Subreddit   : " + childrenList.get(i).getData().getSubreddit() +
                            "--------------------------------------------------------------\n");

                }
            }

            @Override
            public void onFailure(Call<json_feed> call, Throwable t) {
                Log.d(TAG, "ERROR = " + t.toString());

            }
        });

}

json_feed class

public class json_feed {
@SerializedName("Data")
@Expose
private Data data;
//below is get, set and return string method
}

数据类

public class Data {
@SerializedName("Children")
@Expose
private ArrayList<Children> children;
//below is get,set and return string
}

儿童班

public class Children {
@SerializedName("Data")
@Expose
private Data data;
//below is get,set and return string
}

儿童 - &gt;数据类

public class Data {

@SerializedName("subreddit")
@Expose
private String subreddit;
//below is get,set and return string
}

0 个答案:

没有答案