【发布时间】:2016-10-30 07:32:23
【问题描述】:
这个简单的 post 方法没有向服务器(servlet)发送任何内容。我可以得到,但我不能发帖!
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL("http://ipaddr:8080/work/wh");
HttpURLConnection client = (HttpURLConnection) url.openConnection();
client.setRequestMethod("POST");
client.setDoOutput(true);
client.setChunkedStreamingMode(0);
client.connect();
OutputStream outputStream = client.getOutputStream();
PrintWriter pw = new PrintWriter(outputStream);
pw.println(System.currentTimeMillis());
pw.println(System.currentTimeMillis());
pw.println(new Float(5.6));
pw.println(new Integer(3));
pw.println(new Integer(2));
pw.println(new Integer(32));
pw.println(new Integer(2));
pw.close();
outputStream.close();
}catch(Exception e){
Log.e("text", "ux " + e.getMessage());
}
return null;
}
如果我尝试读取任何行 new Scanner(request.getInputStream()).nextLine(); 在服务器端,我会得到 p>
NoSuchElementException:找不到行
你认为可能是什么问题?
【问题讨论】:
-
NoSuchElementException不是“无效”。 -
我的意思是我得到 void object inputsteam 没有数据
-
代码不是MCVE格式,目前提供的信息中看不到原因,所以我们只能给出有根据的猜测。您是否知道请求正文已经(隐式)预先被服务器解析时将为空?例如。调用
request.getParameter()将导致它立即被解析。尽管如此,您为什么不遵循 HTML 表单使用的默认application/x-www-form-urlencoded格式并由 servlet API 原生支持呢?另见stackoverflow.com/q/2793150
标签: java android tomcat servlets httpurlconnection