java.io.EOFException从服务器读取输入流时

时间:2016-03-06 09:53:22

标签: android http exception-handling httpresponse httpurlconnection

连接到服务器时遇到问题,它会产生EOFException并且程序卡在那里。 下面的堆栈跟踪:

java.io.EOFException
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:95)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)

源代码是:

        protected String doInBackground(String... params) {
        HttpURLConnection conn = null;

        try {
            // create connection
            URL wsURL=new URL(params[0]);
            conn=(HttpURLConnection) wsURL.openConnection();
            conn.setConnectTimeout(15000);;
            conn.setReadTimeout(10000);

            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setUseCaches(false);

            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("un", USERNAME);
            String data = builder.build().getEncodedQuery();
            byte[] outputInBytes = data.getBytes("UTF-8");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(outputInBytes.length));
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(outputInBytes);
            os.close();

            // get data
            InputStream in = new BufferedInputStream(conn.getInputStream());

            // converting InputStream into String
            Scanner scanner = new Scanner(in);
            String strJSON = scanner.useDelimiter("\\A").next();
            scanner.close();
            return strJSON;

我在行中获得此异常...我在InputStream中获取数据...

  

InputStream in = new BufferedInputStream(conn.getInputStream());

0 个答案:

没有答案