gpt4 book ai didi

android - 改造不解析数据

转载 作者:行者123 更新时间:2023-11-29 17:18:42 25 4
gpt4 key购买 nike

我正在尝试使用 Retrofit 访问 Google News API,但在我的实现中遇到了问题,如果更有经验的人能看一眼我的代码,我将很高兴。问题是查询已成功构建并进入 response.isSuccessful(),但它没有任何条目。

我已确保 API 正常工作 ( http://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=technology )。此外,通过使用参数构建查询来使用 Postman 进行测试: enter image description here

我用 http://www.jsonschema2pojo.org/ 生成了我的 pojo .以下是类(class):

NewsEntryApiEntry

public class NewsApiEntry {
@SerializedName("url")
@Expose
private String url;
@SerializedName("title")
@Expose
private String title;
@SerializedName("contentSnippet")
@Expose
private String contentSnippet;
@SerializedName("link")
@Expose
private String link;

/**
*
* @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 contentSnippet
*/
public String getContentSnippet() {
return contentSnippet;
}

/**
*
* @param contentSnippet
* The contentSnippet
*/
public void setContentSnippet(String contentSnippet) {
this.contentSnippet = contentSnippet;
}

/**
*
* @return
* The link
*/
public String getLink() {
return link;
}

/**
*
* @param link
* The link
*/
public void setLink(String link) {
this.link = link;
}

}

NewsApiResult

public class NewsApiResult {
@SerializedName("query")
@Expose
private String query;
@SerializedName("entries")
@Expose
private List<NewsApiEntry> entries = new ArrayList<NewsApiEntry>();

/**
*
* @return
* The query
*/
public String getQuery() {
return query;
}

/**
*
* @param query
* The query
*/
public void setQuery(String query) {
this.query = query;
}

/**
*
* @return
* The entries
*/
public List<NewsApiEntry> getEntries() {
return entries;
}

/**
*
* @param entries
* The entries
*/
public void setEntries(List<NewsApiEntry> entries) {
this.entries = entries;
}

}

改造服务生成器

public class NewsApiServiceGenerator {

public static final String API_BASE_URL = "http://ajax.googleapis.com/ajax/services/feed/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();

private static Retrofit.Builder retroBuilder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson));

public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = retroBuilder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}

最后,MainActivity:

public class MainActivity extends AppCompatActivity {
public static List<NewsApiEntry> newsArticles;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

newsArticles= new ArrayList<>();
fetchNewsArticles("technology");
}

private void fetchNewsArticles(String topic) {
NewsApiClient client = NewsApiServiceGenerator.createService(NewsApiClient.class);

Call<NewsApiResult> call = client.fetchNews("1.1", topic);
call.enqueue(new Callback<NewsApiResult>() {
@Override
public void onResponse(Call<NewsApiResult> call, Response<NewsApiResult> response) {
if(response.isSuccessful()) {
NewsApiResult res = response.body();
Log.d("API REQUEST URL: ", call.request().url().toString());
Log.d("API RESPONSE: ", response.raw().toString());
Log.d("API ENTRY COUNT: ", Integer.toString(res.getEntries().size()));
for(NewsApiEntry newsArticle : res.getEntries()) {
Log.d("API NEWS ENTRY TITLE: ", newsArticle.getTitle());
newsArticles.add(newsArticle);
}
} else {
int statusCode = response.code();
ResponseBody errorBody = response.errorBody();
Log.e("Network Error: ", errorBody.toString());
}
}

@Override
public void onFailure(Call<NewsApiResult> call, Throwable t) {
t.printStackTrace();
}
});
}
}

这是日志: enter image description here

查询已正确构建,响应的状态为 200,但为什么我没有收到充满条目的正文?注意:我的库已正确加载并且项目构建成功。

最佳答案

{"responseData": {"query":"technology","entries":[...]}}

看看你的NewsApiResult类是包含在 responseData 中的映射属性外部对象的属性。您还需要将该外部对象映射到一个新类,如下所示:

public class NewsApiResponse {
@SerializedName("responseData")
@Expose
private NewsApiResult responseData;
...
}

并且您的 Retrofit 调用必须这样声明:

Call<NewsApiResponse> call = client.fetchNews("1.1", topic);

这应该可以解决您的问题。

关于android - 改造不解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37605505/

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