English 中文(简体)
http://www.un.org。 安妮- j瓦服务器(喷气机)提出的员额要求:参数丢失?
原标题:httpPost request from Android to java server (jetty): parameters get lost?

我是在

HttpPost httpPost = new HttpPost(params[0]);

HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("title", "message");

//... setting some other parameters like http timeout, which I checked and which work 

httpPost.setParams(httpParams);

//HttpEntity myEntity = new StringEntity(messageBody);
//httpPost.setEntity(myEntity);

response = httpClient.execute(httpPost);

(the commented part is something I tried as well, but with no results).

The server code looks like this:

public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
    response.setContentType("text/plain;charset=utf-8");
    if (target.contentEquals("/postKdm"))
    {
        String title = request.getParameter("title");

        InputStream instream = request.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line);
        }

        System.out.println(title);

        response.setStatus(HttpServletResponse.SC_OK);
    }
}

Where both the String title and the InputStream are null/empty. I ve debugged and checked the request object, but couldn t find anything looking like my parameter.

Also, I found something sounding similar to my problem here, but the answer didn t help me much as I don t work with Apache Camel and therefore can t use the Exchange class.

Oh, and the similar GET request is working excellent, but here I just got stuck. :/

我感谢任何帮助!

Kind regards, jellyfish

最佳回答

我仍然不知道“板块”为什么不工作。 但是,我用电灯对我即将提出的要求进行了检查,此后,我找到了使用HttpEntity(如上文评论部分所述)的解决办法:

HttpEntity myEntity = new StringEntity(message);
httpPost.setEntity(myEntity);

response = httpClient.execute(httpPost);

The server side I found out thanks to Chris answer, only that I, of course, replaced the byte buffer with a char buffer, like here:

private String getInputString() throws IOException
{
    InputStream is = request.getInputStream();
    if (is != null)
    {

        Writer writer = new StringWriter();

        char[] buffer = new char[request.getContentLength()];

        try
        {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            int n;
            while ((n = reader.read(buffer)) != -1)
            {
                writer.write(buffer, 0, n);
            }
        }
        finally
        {
            is.close();
        }
        return writer.toString();
    }
    else
    {
        return "";
    }
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签