2017-01-26 10 views
1

私は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' 
+0

どちらか 'onResponse'または' onFailure'を呼び出さなければなりません。アプリがクラッシュしますか? – eleven

答えて

2

​​のスペースを削除します。 これは問題の可能性があります。

更新

HttpLoggingInterceptorを実装します。このようにして、GETが表示されます。ここでは、コードの小さな助けです。

public static OkHttpClient GetClient(){ 
     HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
     logging.setLevel(HttpLoggingInterceptor.Level.BODY); 
     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
     return httpClient.addInterceptor(logging).build(); 
     } 

そして改造でこのようになります...

Retrofit retrofit = new Retrofit.Builder() 
       .client(GetClient()) 
       .baseUrl("https://newsapi.org/v1/") 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
+0

これはうまくいきました...ログの猫の応答を見ることができます –

+0

私はこのJsonを見せたいリストビューのデータ 私のカスタムアレイアダプタを持っています どうすればいいですか? –

+0

@OmerDanishさて、聞いてよかったです... 今すぐ教えてください... '最後の記事記事= response.body();'これであなただけの記事や記事のリストを取得していますか? 基本コードは... 'CustomListAdapter customAdapter = new CustomListAdapter(this、R.layout.your_custom_item_row、yourListView); yourListView .setAdapter(customAdapter); ' – FiN

1

@GET("/articles")からスラッシュを削除します。これにより、最終的なURLがhttps://newsapi.org/v1//articlesになります。あなたは改装を施工するためにhttps://newsapi.org/v1/を使用しています。

+0

ありがとうございました BaseURL "= https://newsapi.org/v1/" @GET( "articles /") –