android - 改造 2 没有给出响应

标签 android retrofit2

我正在使用 Retrofit 2.0
API:https://newsapi.org/v1/articles ?
API接口(interface):

public interface NewsAPI {
    // source : the-next-web
    // SortBy : latest
    @GET("/articles")
    Call<Article> articles(
            @Query("source") String source,
            @Query("apiKey") String apiKey);
}

fragment :

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: No virtual method log(Ljava/lang/String;)V in class Lokhttp3/internal/Platform;或其父类(super class)

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'

最佳答案

去掉.baseUrl("https://newsapi.org/v1/")中的空格。 这可能是问题所在。

更新

实现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();

关于android - 改造 2 没有给出响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872371/

相关文章:

android - 无法解析方法订阅 Retrofit Rxjava android

java - 在Java中传递动态类对象

c# - 如何在打开指定 URL 时关闭 WebViewClient

android - ImageButton 在 TranslateAnimation 之后移动后的 OnClickListener 问题

java - Android to servlet 图片上传保存到服务器

android - 如何使用 Retrofit 2 从数据库中获取图像

java - 需要带引号的字符串(位置 :DOCDECL @1:50 in java. io.StringReader@4327b040) - Android

android - Android 中的简单 XML 解析

java - 使用 Uri 创建文件后文件为空 - Retrofit

android - 如何使用 retrofit2 创建 json 对象数组的查询