各位高手,小的有一个问题想要请教各位
在一次使用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();
求解???