gpt4 book ai didi

java - 无法使用 outputStreamWriter() 将字符串从应用程序发送到 servlet

转载 作者:行者123 更新时间:2023-12-01 13:32:19 26 4
gpt4 key购买 nike

我正在尝试将一个小字符串从我的应用程序发送到 Java servlet:

//Send user mobile number to the server.
EditText cinMobile = (EditText)findViewById(R.id.mobile_no);
String inputVal = cinMobile.getText().toString();

URL mobileurl = new URL(url);
URLConnection connection = mobileurl.openConnection();
connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(inputVal);
out.close();

Servlet 应该通过 doPost() 接收值:

//Accept the phone number form the app
int length = request.getContentLength();
byte[] input = new byte[length];
ServletInputStream sin = request.getInputStream();
int c, count = 0;

while((c = sin.read(input, count, input.length-count)) != -1) {
count = +c;
}

sin.close();

String receivedString = new String(input);
response.setStatus(HttpServletResponse.SC_OK);

System.out.print(receivedString);

但是当我运行命令并尝试发送字符串时,我在 servlet 端收到 byte[] input = new byte[length] 行的负数组大小异常 ;:

Feb 01, 2014 2:06:02 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [project_server.Login] in context with path [/project_server] threw exception
java.lang.NegativeArraySizeException
at project_server.Login.doPost(Login.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

我不确定这里出了什么问题,但该错误意味着它没有收到任何字符串。LogCat 显示应用程序端没有错误。

最佳答案

I'm not sure what is wrong here but the error implies that it is not receiving any string.

不,该错误意味着您正在尝试创建一个负长度的数组。这意味着这些行:

int length = request.getContentLength();                                
byte[] input = new byte[length];

可能未设置 Content-Length header ,因此 getContentLength() 返回 -1。您应该能够处理这个问题 - 只需从输入流中读取,直到 read() 返回非正数。例如,您可以使用 ByteArrayOutputStream 复制数据。或者,您可以只使用 StringBuilder 并围绕输入流创建一个 InputStreamReader:

char[] buffer = new char[8192];
StringBuilder builder = new StringBuilder();
try (Reader reader = new InputStreamReader(request.getInputStream(), "UTF-8")) {
int charsRead;
while ((charsRead = reader.read(buffer)) > 0) {
builder.append(buffer, 0, charsRead);
}
}
String text = builder.toString();

请注意我如何指定 UTF-8 作为输入编码 - 您在写入数据时也应该指定相同的编码。目前,您依赖于客户端上与服务器上相同的平台默认编码,这是一个坏主意。

关于java - 无法使用 outputStreamWriter() 将字符串从应用程序发送到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com