【问题标题】:Retrofit,onResponse method doesnt work改造,onResponse 方法不起作用
【发布时间】:2016-08-15 04:08:20
【问题描述】:

我是 Retrofit 的新手,尝试从一台 Web 服务器获取数据,创建模型,接口,但这仍然无法正常工作。问题(可能)在方法 onResponse() 我添加到该方法 Log.d 和 Toast 但我没有看到启动我的应用程序时记录和敬酒。为什么这不起作用?当我得到错误的响应或其他东西时,我可以理解,但是 onResponse() 通常不起作用,我怎么想。也许 Toast 没有数据就无法工作,但是 Log.d 必须在没有数据的情况下工作,而 Log.d 没有数据,只是代码的回应。我添加了所有依赖项并尝试在所有教程中这样做,我做错了什么以及我能做些什么来解决这个问题?而且我尝试将这些数据放入适配器,但是当启动应用程序时,我在日志中出现错误“RecyclerView:未连接适配器;跳过布局”可能是同样的问题。onResponse 不起作用并且适配器没有创建,因为适配器初始化在 onResponse 方法中,如果 onResponse 不起作用,recyclerview 的 setadapter 不起作用。并且 VideoApi 类:

public interface VideoApi {

    @GET("/videos/featured")
    Call<List<Video>>getFeaturedVideo();
}

视频类:

public class Video {

    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("score")
    @Expose
    private Integer score;

    /**
     *
     * @return
     * The url
     */
    public String getUrl() {
        return url;
    }

    /**
     *
     * @param url
     * The url
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     *
     * @return
     * The title
     */
    public String getTitle() {
        return title;
    }

    /**
     *
     * @param title
     * The title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     *
     * @return
     * The description
     */
    public String getDescription() {
        return description;
    }

    /**
     *
     * @param description
     * The description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     *
     * @return
     * The score
     */
    public Integer getScore() {
        return score;
    }

    /**
     *
     * @param score
     * The score
     */
    public void setScore(Integer score) {
        this.score = score;
    }

}

精选片段:

public class FeaturedFragment extends Fragment {
    RecyclerViewAdapter recyclerViewAdapter;
    public static final String ROOT_URL = "https://api.vid.me/";
    public List <Video> videos;
    RecyclerView recList;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_featured, container, false);
        recList = (RecyclerView) rootView.findViewById(R.id.cardList);
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);
        try {
            getVideos();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rootView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void getVideos() throws IOException {
        Retrofit retrofitAdapter = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ROOT_URL)
                .build();
        final VideoApi videoApi = retrofitAdapter.create(VideoApi.class);
Call<List<Video>> call = videoApi.getFeaturedVideo();
        call.enqueue(new Callback<List<Video>>() {
            @Override
            public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {
                Log.d("MainActivity", "Status Code = " + response.code());
                videos.addAll(response.body());
                recyclerViewAdapter = new RecyclerViewAdapter(videos);
                String result = response.body().get(0).getTitle();
                Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();
                recList.setAdapter(recyclerViewAdapter);
            }

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

            }
        });

    }
}

【问题讨论】:

  • 您是否在 Manifest 文件中声明了 INTERNET 权限?
  • 我当然做到了。在清单中放置了

标签: android retrofit android-recyclerview retrofit2


【解决方案1】:

您的 json 响应返回 Video 对象的数组。 将Call 对象中的List&lt;Video&gt; 更改为Videos 其中Videosclass 被定义为 -

public class Videos {
    List<Video> videos;
}

这样改——

Call<Videos> call = videoApi.getFeaturedVideo();
        call.enqueue(new Callback<Videos>() {
            @Override
            public void onResponse(Call<Videos> call, Response<Videos> response) {
                Log.d("MainActivity", "Status Code = " + response.code());
                videos = response.body().videos;
                recyclerViewAdapter = new RecyclerViewAdapter(videos);                   
                recList.setAdapter(recyclerViewAdapter);
            }

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

            }
        });

    }

另外,改变-

@GET("/videos/featured")
Call<Videos>getFeaturedVideo();

【讨论】:

  • 我不明白你的意思,对不起
  • 为什么会这样?实际上另一个声明也应该可以正常工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多