私はRetrofit 2.0を使用しています
API:https://newsapi.org/v1/articles?
APIインタフェース:改造2応答がない
public interface NewsAPI {
// source : the-next-web
// SortBy : latest
@GET("/articles")
Call<Article> articles(
@Query("source") String source,
@Query("apiKey") String apiKey);
}
フラグメント:いいえ仮想メソッドログ(Ljava:
public class ArticlesFragment extends Fragment {
private NewsAPI newsAPI;
public ArticlesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list, container, false);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(" https://newsapi.org/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
newsAPI = retrofit.create(NewsAPI.class);
loadArticle();
return rootView;
}
public void loadArticle() {
Call<Article> call =
newsAPI.articles("the-next-web", "apiKey");
call.enqueue(new Callback<Article>() {
@Override
public void onResponse(Call<Article> call, Response<Article> response) {
final Article article = response.body();
if (article != null) {
Log.v("this", "YES! response");
}
}
@Override
public void onFailure(Call<Article> call, Throwable t) {
Log.v("this", "NO response ");
}
});
}
更新
使用迎撃v3.4.1そうしないと、エラー - java.lang.NoSuchMethodErrorのを次れます/ lang/String;)クラスLokhttp3/internal/PlatformのV。または2.1.0のためにそのスーパークラス
の構成は次のようになります。
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
どちらか 'onResponse'または' onFailure'を呼び出さなければなりません。アプリがクラッシュしますか? – eleven