java - 为什么我要关闭 Java.io.IOException 流?

标签 java android httpurlconnection

我不确定为什么会收到此错误。我尝试在网上搜索答案,但没有成功。

下面是发生错误的部分代码。我正在发送 header ,在获得 200 的成功响应后,我将执行其余代码。

URL url;
        HttpURLConnection connection = null;        
        try{


//Create connection

        url = new URL(targetURL);
        Log.i(TAG,"Connecting to : "+targetURL);
        connection = (HttpURLConnection)url.openConnection();

        //connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", 
                   "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Language", "en-US"); 
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");


        connection.setRequestProperty("x-device-displayheight", device_displayheight );

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuffer response = new StringBuffer();
            String rep = connection.getResponseMessage();
            int respcode = connection.getResponseCode();
            String line;

        try{                        
            if(respcode == 200 ){// Everything is ok                            
                if(is != null){
                    //response.append(is.toString());   

                    while((line = br.readLine()) != null){
                        Log.i("Output",line);
                    }

                }
                else{
                    Log.i("Nothing to parse","");
                }


            }
            else{// bad response from server
                Log.i("Bad Response", rep +" : "+respcode);
            }
        }catch(Exception e){
            e.printStackTrace();
        }

//堆栈跟踪

04-04 13:14:41.668: W/System.err(23046): java.io.IOException: stream closed
04-04 13:14:41.668: W/System.err(23046):    at org.apache.harmony.luni.internal.net.www.protocol.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:69)
04-04 13:14:41.668: W/System.err(23046):    at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:41)
04-04 13:14:41.668: W/System.err(23046):    at java.io.InputStreamReader.read(InputStreamReader.java:248)
04-04 13:14:41.668: W/System.err(23046):    at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
04-04 13:14:41.668: W/System.err(23046):    at java.io.BufferedReader.readLine(BufferedReader.java:357)
04-04 13:14:41.668: W/System.err(23046):    at com.ameba.api.network.Client.executePost(Client.java:95)
04-04 13:14:41.668: W/System.err(23046):    at com.ameba.api.activityClasses.Login$connectToServer.doInBackground(Login.java:190)
04-04 13:14:41.672: W/System.err(23046):    at com.ameba.api.activityClasses.Login$connectToServer.doInBackground(Login.java:1)
04-04 13:14:41.672: W/System.err(23046):    at android.os.AsyncTask$2.call(AsyncTask.java:252)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
04-04 13:14:41.672: W/System.err(23046):    at java.lang.Thread.run(Thread.java:1020)

最佳答案

System.setProperty("http.keepAlive", "false"); 好像解决了问题。

关于java - 为什么我要关闭 Java.io.IOException 流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016842/

相关文章:

java - 将 ASCII 值写入 RS485 等串行通信设备的寄存器

java - 当 "parent"表具有复合 PK 时,如何在 JPA 中建模一对一关系?

eclipse - 我是否应该担心 DDMS 控制台日志消息 "Can' t 绑定(bind)到本地 nnnn 以供调试器使用”?

java - 为什么 BufferedInputStream 一次最多读取 2048 个字节?

java - 将spymemcached Java 客户端GetFuture 适配为Guava ListenableFuture

java - 日版字体错误

java - 如何减去2小时

android - 将参数传递给嵌套图 startDestination

java - 使用 httpurlconnection 发布到 Web 服务

java - 如何在java中将自定义请求位置放入HttpURLConnection中