gpt4 book ai didi

android - 改造 2 没有给出响应

转载 作者:行者123 更新时间:2023-11-29 17:05:23 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com