2016-04-21 47 views
0

Im新的改造,试图从一个Web服务器获取数据,创建模型,接口,但这仍然不工作。问题(也许)在方法onResponse()我添加到该方法Log.d和吐司,但我没有看到日志和吐司时启动我的应用程序。为什么不工作?我可以理解,当我得到错误的响应或其他什么,但onResponse()不工作一般,我的想法。可能Toast不能工作有数据,但Log.d必须没有它的工作,和Log.d没有数据,只是代码的回应。我添加了所有的依赖和tryind在所有教程中都这样做,我做了什么错误以及我能做些什么来解决这个问题?我也试着把这些数据放到适配器上,但是当启动应用程序时,我在Log“RecyclerView:没有附加适配器;跳过布局”中出现错误,也许这是同样的问题.onResponse不工作,适配器也不会创建,因为适配器inilialze在onResponse方法,如果onResponse不起作用,setadapter到recyclerview不起作用to.And VideoApi类:改造,onResponse方法不工作

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

} 

FeaturedFragment:

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

      } 
     }); 

    } 
} 
+0

你声明您的Manifest文件中有INTERNET权限? –

+0

当然我做了它。在清单中推荐。 –

回答

2

你的json响应返回数组Video对象。 变化List<Video>无处不在Call对象Videos 其中Videos类被定义为 -

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(); 
+0

我不明白你的意思,对不起 –

+0

更新了答案。 –

+0

谢谢,它的作品! –