设置postDelayed Runnable时,BufferedInputStream将关闭

时间:2016-03-04 12:55:55

标签: java android json

我的GameActivity是一个简单的测验。我设置了两个Runnable,mRunnableQuestionmRunnableQuestionWithPenalty。逻辑是:mRunnableQuestion显示新问题并等待30秒,当用户在此时间没有回答时,调用另一个Runnable,这会给用户点数惩罚:

private Runnable mRunnableQuestion = new Runnable() {
    @Override
    public void run() {
        new AsyncAPIGetQuestion().execute(mCategoryId);
        mHandler.postDelayed(mRunnableQuestionWithPenalty, 30000);
    }
};

当然,当用户及时回答时,我会给用户点并重新启动Runnable:

mHandler.removeCallbacksAndMessages(null);
mHandler.post(mRunnableQuestion);

效果很好。但是,我想根据正确/错误的答案给卡片上色并等待5秒钟。所以我将上面的代码更改为:

mHandler.removeCallbacksAndMessages(null);
colorAnswers();
mHandler.postDelayed(mRunnableQuestion, 5000);

这就是问题的开始。当我运行我的应用程序时,遇到2,3或4个问题后,我收到EOF / BufferedInputStream错误:

    03-04 13:38:24.107 16506-16532/com.my.pkg W/System.err: java.io.EOFException
    03-04 13:38:24.107 16506-16532/com.my.pkg W/System.err:     at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:95)
    03-04 13:38:24.107 16506-16532/com.my.pkg W/System.err:     at com.
    android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
    03-04 13:38:24.107 16506-16532/com.my.pkg W/System.err:     at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
    03-04 13:38:24.107 16506-16532/com.my.pkg W/System.err:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
    03-04 13:38:24.107 16506-16532/com.my.pkg W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
    03-04 13:38:24.107 16506-16532/com.my.pkg W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at com.my.pkg.JSONParser.getJSONFromUrl(JSONParser.java:45)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at com.my.pkg.GameActivity$AsyncAPIGetQuestion.doInBackground(GameActivity.java:317)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at com.my.pkg.GameActivity$AsyncAPIGetQuestion.doInBackground(GameActivity.java:299)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    03-04 13:38:24.108 16506-16532/com.my.pkg W/System.err:     at java.lang.Thread.run(Thread.java:818)
    03-04 13:38:24.108 16506-16532/com.my.pkg E/JSONParser.java: Error converting result java.io.IOException: BufferedInputStream is closed

当我在那里添加额外的postDelayed()时,它才会发生。之前没有问题。

我的doInBackground是由Runnable执行的AsyncTask的一部分,它返回如上所述的错误

    @Override
    protected JSONArray doInBackground(String... params) {
        JSONParser jParser = new JSONParser();
        JSONArray json = jParser.getJSONFromUrl(apiURL);
        return json;
    }

JSONParser.getJSONFromURL

public JSONArray getJSONFromUrl(String urlSource) {
    try {
        URL url = new URL(urlSource);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);
        inputStream = new BufferedInputStream(urlConnection.getInputStream());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
            if(!reader.ready()) {
                break;
            }
        }
        inputStream.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error converting result " + e.toString());
    }

    try {
        jArr = new JSONArray(json);
    } catch (JSONException e) {
        Log.e(TAG, "Error parsing data " + e.toString());
    }

    return jArr;
}

我真的很感激任何帮助。一切顺利!

1 个答案:

答案 0 :(得分:0)

看起来我解决了这个问题。

问题出在我的jParser.getJSONFromUrl()方法中,特别是这些行:

URL url = new URL(urlSource);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);

当您使用POST / PUT请求时,似乎setDoOutput参数设置为true。如果您只想执行GET请求,例如在我的情况下,应将其设置为false

urlConnection.setDoOutput(false);

否则,您最终可能会收到错误,如上所述。

向社区致以最诚挚的问候!