Ich bin neu in Retrofit, versuchen, Daten von einem Webserver zu erhalten, erstellen Model, Interface, aber das funktioniert immer noch nicht.Problem (vielleicht) in der Methode onResponse() Ich füge diese Methode Log.d und Toast, aber ich sehe nicht Log und Toast beim Start meiner App.Warum das nicht funktionieren? Ich kann verstehen, wenn ich falsche Antwort oder etwas anderes bekomme, aber onResponse() funktioniert nicht im Allgemeinen, wie ich denke.Maybe Toast kann nicht ohne Daten arbeiten, aber Log.d muss ohne sie arbeiten, und Log.d havnt Daten, nur Code der Antwort. Ich habe alle Abhängigkeiten hinzugefügt und versuche es wie in allen Tutorials, was habe ich falsch gemacht und was kann ich tun, um das zu beheben? Und ich versuche auch, diese Daten auf Adapter, aber wenn Start-App, habe ich Fehler in Log "RecyclerView: Kein Adapter angeschlossen; Layout überspringen" vielleicht ist es das gleiche Problem.aufReaktion nicht funktionieren und Adapter nicht erstellen, weil Adapter inilialze in onResponse Verfahren und wenn onResponse nicht funktioniert, setAdapter zu recyclerview funktioniert nicht to.And VideoApi Klasse:Retrofit, onResponse-Methode funktioniert nicht
public interface VideoApi {
@GET("/videos/featured")
Call<List<Video>>getFeaturedVideo();
}
Video-Klasse:
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) {
}
});
}
}
Haben Sie erklärt INTERNET-Berechtigung in Ihrer Manifest-Datei? –
natürlich habe ich es getan.Puted im Manifest. –