从输入流读取时出错

时间:2017-12-08 00:27:43

标签: android

我正在尝试根据Hacker News的API制作新闻阅读器应用程序。当我试图让InputStream从连接中获取InputStream时,但有些事情发生了,我无法真正理解它。

public class MainActivity extends AppCompatActivity {

    ArrayList<String> newsArrayList;
    ArrayAdapter<String> arrayAdapter;
    ArrayList<String> arrayURLs;
    int numberOfItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        newsArrayList = new ArrayList<String>();
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, newsArrayList);
        arrayURLs = new ArrayList<>();

        final ListView listView = (ListView) findViewById(R.id.newsList);
        listView.setAdapter(arrayAdapter);

        DownloadTask task = new DownloadTask();

        try {
            task.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty").get();

            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                    if(newsArrayList.size() == arrayURLs.size()){
                        Intent intent = new Intent(MainActivity.this, WebActivity.class);

                        WebActivity.webView.loadUrl(arrayURLs.get(position));

                        startActivity(intent);
                    }
                    return true;
                }
            });

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        Log.i("a","a");


    }


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

        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection connection;

            try {
                url = new URL(urls[0]);
                connection = (HttpURLConnection) url.openConnection();

                InputStream in = connection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);

                int data = reader.read();

                while(data != -1){

                    char part = (char) data;
                    result = result + part;

                    data = reader.read();
                }

                JSONArray jsonArray = new JSONArray(result);

                numberOfItems = 20;
                for (int i = 0; i < numberOfItems; i++ ){
                    String getId = jsonArray.getString(i);

                    url = new URL("https://hacker-news.firebaseio.com/v0/item/" + getId + ".json?print=pretty");
                    connection = (HttpURLConnection) url.openConnection();

                    in = connection.getInputStream();
                    reader = new InputStreamReader(in);

                    data = reader.read();
                    String articleInfo = "";
                    while(data != -1){

                        char part = (char) data;
                        articleInfo = articleInfo + part;

                        data = reader.read();
                    }
                    JSONObject jsonObject = new JSONObject(articleInfo);

                    String articleTitle = jsonObject.getString("title");
                    String articleURL = jsonObject.getString("url");

                    arrayURLs.add(articleURL);

                    newsArrayList.add(articleTitle);
                    arrayAdapter.notifyDataSetChanged();
                }




                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }
    }
}

错误:

 12-07 11:03:00.294 2692-7601/com.google.android.googlequicksearchbox:search W/ErrorReporter: reportError [type: 211, code: 524300]: Error reading from input stream
12-07 11:03:00.296 2692-7601/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
                                                                                              com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
                                                                                                  at com.google.android.apps.gsa.staticplugins.recognizer.i.a.a(SourceFile:342)
                                                                                                  at com.google.android.apps.gsa.staticplugins.recognizer.i.a$1.run(SourceFile:1367)
                                                                                                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                                  at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
                                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                                  at java.lang.Thread.run(Thread.java:761)
                                                                                                  at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
                                                                                               Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
                                                                                                  at com.google.android.apps.gsa.speech.audio.Tee.g(SourceFile:2531)
                                                                                                  at com.google.android.apps.gsa.speech.audio.ap.read(SourceFile:555)
                                                                                                  at java.io.InputStream.read(InputStream.java:101)
                                                                                                  at com.google.android.apps.gsa.speech.audio.al.run(SourceFile:362)
                                                                                                  at com.google.android.apps.gsa.speech.audio.ak$1.run(SourceFile:471)
                                                                                                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                                  at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
                                                                                                  at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
                                                                                                  at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
                                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                                                  at java.lang.Thread.run(Thread.java:761) 
                                                                                                  at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)

0 个答案:

没有答案
相关问题