首页 > 解决方案 > 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;
    }
}

标签: javaandroidandroid-volley

解决方案


推荐阅读