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) {
}
});
}
}
你聲明您的Manifest文件中有INTERNET權限? –
當然我做了它。在清單中推薦 。 –