首页 新闻 会员 周边 捐助

HttpUrlConnection中getInputStream出现问题?

0
悬赏园豆:20 [待解决问题]

各位高手,小的有一个问题想要请教各位
在一次使用HTTPURLConnection来连接服务器时,getInputStream出现的错误,但是outStream却又是正常(服务器得到了数据)代码如下:

public void getServerData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        try {
                            String urlpath = Addr.Url+"GetmsgServlet";
                            URL url = new URL(urlpath);
                            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                            connection.setConnectTimeout(10000);
                            connection.setUseCaches(false);
                            connection.setInstanceFollowRedirects(true);
                            connection.setDoOutput(true);
                            connection.setDoInput(true);
                            connection.setRequestMethod("POST");
                            connection.setRequestProperty("Content-Type","application/json");
                            connection.setRequestProperty("charset","UTF-8");
                            connection.setRequestProperty("accept","application/json");
                            InputStream inputStream = connection.getInputStream();
                            OutputStream outputStream = connection.getOutputStream();
                            StringBuilder json = new StringBuilder();
                            json.append('[');
                            json.append("{");
                            json.append("lastMsgNum:").append(lastMsgNum).append(",");
                            json.append("receiver:").append(user).append(",");
                            json.append("sender:").append(receiver.getAccount()).append(",");
                            json.append("pubkey:").append(pubkey).append("}");
                            json.append(']');
                            byte[] msg = json.toString().getBytes();
                            outputStream.write(msg);
                            outputStream.flush();
                            byte[] data = StreamTool.read(inputStream);
                            String jsons = new String(data);
                            if (!"]".equals(jsons)) {
                                Message message = new Message();
                                message.obj = jsons;
                                handler.sendMessage(message);
                            }
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                },0,5000);
            }
        }).start();
    }

我想用轮询的当时来访问服务器当时却报如下错误:

其中193行代码为:InputStream inputStream = connection.getInputStream();
求解???

Wb同学在此的主页 Wb同学在此 | 初学一级 | 园豆:7
提问于:2019-06-25 00:34
< >
分享
所有回答(1)
0
小光 | 园豆:1766 (小虾三级) | 2019-06-25 09:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册