java - Volley StringRequest 结果为空错误
问题描述
我最近想创建一个定位和显示新闻来源的程序。我所做的是使用一个名为 news api 的 api,并使用 android volley 模块进行字符串请求,但是当我尝试运行应用程序时,它在我在 Response.ErrorListener() 中设置的 toast 上给出了一个空错误。我还验证了我的 api 密钥,没有任何问题。感谢所有未来的帮助。这是代码:
MainActivity.java:
private void loadSources() {
sourcesLists = new ArrayList<>();
sourcesLists.clear();
progressBar.setVisibility(View.VISIBLE);
String url = "https://newsapi.org/v2/sources?apiKey=" + Constants.API_KEY;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("sources");
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String id = jsonObject1.getString("id");
String name = jsonObject1.getString("name");
String description = jsonObject1.getString("description");
String url = jsonObject1.getString("url");
String country = jsonObject1.getString("country");
String category = jsonObject1.getString("category");
String language = jsonObject1.getString("language");
SourcesListModel sourcesListModel = new SourcesListModel(
""+id,
""+name,
""+description,
""+url,
""+category,
""+language,
""+country
);
sourcesLists.add(sourcesListModel);
}
sourcesListAdapter = new SourcesListAdapter(MainActivity.this, sourcesLists);
sourcesRecyclerView.setAdapter(sourcesListAdapter);
}
catch (Exception e) {
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
My Adapter:
public class SourcesListAdapter extends RecyclerView.Adapter<SourcesListAdapter.SourcesListHolder> {
private Context context;
private ArrayList<SourcesListModel> sourcesLists;
public SourcesListAdapter(Context context, ArrayList<SourcesListModel> sourcesLists) {
this.context = context;
this.sourcesLists = sourcesLists;
}
@NonNull
@Override
public SourcesListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.row_source_list, parent, false);
return new SourcesListHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull SourcesListAdapter.SourcesListHolder holder, int position) {
SourcesListModel model = sourcesLists.get(position);
String id = model.getId();
String name = model.getName();
String description = model.getDescription();
String country = model.getCountry();
String category = model.getCategory();
String language = model.getLanguage();
holder.nameTextView.setText(name);
holder.descriptionTextView.setText(description);
holder.countryTextView.setText(context.getString(R.string.country_text)+ country);
holder.categoryTextView.setText(context.getString(R.string.category_text)+ category);
holder.languageTextView.setText(context.getString(R.string.language_text)+ language);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return sourcesLists.size();
}
class SourcesListHolder extends RecyclerView.ViewHolder{
TextView nameTextView, descriptionTextView, countryTextView, categoryTextView, languageTextView;
public SourcesListHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
descriptionTextView = itemView.findViewById(R.id.descriptionTextView);
countryTextView = itemView.findViewById(R.id.countryTextView);
categoryTextView = itemView.findViewById(R.id.categoryTextView);
languageTextView = itemView.findViewById(R.id.languageTextView);
}
}
}
My Model:
public class SourcesListModel {
String id, name, description, url, category, language, country;
public SourcesListModel(String id, String name, String description, String url, String category, String language, String country) {
this.id = id;
this.name = name;
this.description = description;
this.url = url;
this.category = category;
this.language = language;
this.country = country;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String id) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
解决方案
推荐阅读
- python - 使用现有列中的特定单词向数据框添加额外列
- akka - 与演员之外的 akka 接待员互动
- flutter - 没有定义“statefulwidget.createstate”和 setstate 的具体实现
- tfs - devops feed 显示来自上游来源的包。我可以禁用它吗?
- python - 带有不等列的 Pandas DataFrame 将用 NA 填充
- c - 理解 Gtk 模板对象
- java - 带弹簧靴的 Hikari cp .. 活动连接始终为 1 .. 不增加但不降低到 0
- c++ - 编译“fopen_s”时出错未在此范围内声明
- ansible - 从引号列表中返回 IP 地址 - Ansible
- sql-server - 使用 Fluentmigrator 转换 SQL 表达式以创建索引