RecyclerView返回'true'而不是JSON

时间:2017-12-21 17:57:32

标签: java android json android-studio

由于某些奇怪的原因,我的RecyclerView返回布尔值'true'。

我似乎无法解决问题

This is the output I am getting

它应该显示来自JSON的文本

JSON LINK - https://newsapi.org/v2/top-headlines?sources=cnn&apiKey=c80ddd850a524fe5975cad881d6f4aba

我正在访问'articles'JSON数组,我想显示标题

MainActivity代码:

public class MainActivity extends AppCompatActivity {


RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
MyAdapter mViewAdapter;

List<News> news_list = new ArrayList<>();

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


    new ParseNewsJSON().execute();

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    //mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    mViewAdapter = new MyAdapter(new ArrayList<News>());
    mRecyclerView.setAdapter(mViewAdapter);

}


public class ParseNewsJSON extends AsyncTask<String, Void, String> {

    private final String JSON_URL = "https://newsapi.org/v2/top-headlines?sources=cnn&apiKey=c80ddd850a524fe5975cad881d6f4aba";

    String result = "";

    ArrayList<String> article_heading = new ArrayList<>();

    @Override
    protected String doInBackground(String... strings) {

        try {
            URL url = new URL(JSON_URL);

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = httpURLConnection.getInputStream();

            InputStreamReader reader = new InputStreamReader(inputStream);

            int data = reader.read();

            while (data != -1) {

                char current = (char) data;

                result += current;

                data = reader.read();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    protected void onPostExecute(String s) {

        try {
            JSONObject jsonObject = new JSONObject(result);

            JSONArray jsonArray = jsonObject.getJSONArray("articles");

            for (int i = 0; i < jsonArray.length(); i++) {

                News news = new News();

                news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));

                // article_heading.add(jsonArray.getJSONObject(i).optString("title"));


                Log.d("news_JSON", article_heading.toString());

                Log.d("news", news.getHeading().toString());
                news_list.add(news);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        mViewAdapter = new MyAdapter(news_list);
        mRecyclerView.setAdapter(mViewAdapter);
        mViewAdapter.notifyDataSetChanged();
        super.onPostExecute(s);
    }
}
}

MyAdapter代码:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<News> newsArticleList;

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView title;

    //description, genre;

    public ViewHolder(View itemView) {
        super(itemView);

        title = (TextView) itemView.findViewById(R.id.textView_articleHeading);
//            description = (TextView) itemView.findViewById(R.id.textView_description);
//            image = (TextView) itemView.findViewById(R.id.rating);
    }
}

public MyAdapter (List<News> newsArticleList){

    this.newsArticleList = newsArticleList;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_main, parent, false);
    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {

    News news = newsArticleList.get(position);


    holder.title.setText(news.getHeading());


}

@Override
public int getItemCount() {
    return newsArticleList.size();
}
}

News.Java;

public class News {

private String heading;
//private String descrpiton;
private String img;


public String getHeading() {
    return heading;
}

public void setHeading(String heading) {
    this.heading = heading;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}
}

4 个答案:

答案 0 :(得分:0)

你的问题在这里

news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));

article_heading是一个ArrayList,方法add返回一个布尔值,表示列表是否因为你的调用而改变了。

您没有添加标题,您正在添加ArrayList方法的返回,您需要将该行更改为

String title = jsonArray.getJSONObject(i).optString("title");
article_heading.add(title);
news.setHeading(title);

答案 1 :(得分:0)

更改你的onPostExecute,如下所示

   protected void onPostExecute(String s) {

        try {
            JSONObject jsonObject = new JSONObject(result);

            JSONArray jsonArray = jsonObject.getJSONArray("articles");

            for (int i = 0; i < jsonArray.length(); i++) {

                News news = new News();

                String headingFromJson  =  jsonArray.getJSONObject(i).getString("title");
                article_heading.add(headingFromJson);
                news.setHeading(headingFromJson);

                // article_heading.add(jsonArray.getJSONObject(i).optString("title"));


                Log.d("news_JSON", article_heading.toString());

                Log.d("news", news.getHeading().toString());
                news_list.add(news);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        mViewAdapter = new MyAdapter(news_list);
        mRecyclerView.setAdapter(mViewAdapter);
        mViewAdapter.notifyDataSetChanged();
        super.onPostExecute(s);
    }

答案 2 :(得分:0)

您正在以错误的方式设置新闻标题。

您应该在Aynctask中执行以下操作:

news.setHeading(jsonArray.getJsonObject(i).getString("title"));

您正在使用add方法将标题放在arraylist中。并将add方法的结果作为标题。 arraylist的add方法返回一个布尔值,指示列表的添加是否成功。因此,你不断变为真实而不是实际的文本

答案 3 :(得分:0)

试试这个:

在你的ParseNewsJSON课程onpostexecute()

更改为:

    for (int i = 0; i < jsonArray.length(); i++) {

                News news = new News();
                news.setHeading(jsonArray.getJSONObject(i).optString("title"));
                news_list.add(news);

    }

而不是:

   for (int i = 0; i < jsonArray.length(); i++) {

            News news = new News();

            news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));

            // article_heading.add(jsonArray.getJSONObject(i).optString("title"));


            Log.d("news_JSON", article_heading.toString());

            Log.d("news", news.getHeading().toString());
            news_list.add(news);

   }
相关问题