简体   繁体   中英

httpPost request from Android to java server (jetty): parameters get lost?

I am sending off a rather simple httpPost request, following this howto . (It's in German, but you can have a look at the HTTP POST example). This is what I got:

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. :/

I would appreciate any kind of help!

Kind regards, jellyfish

I still don't know why the "setParams" doesn't work. But I used Wireshark to check my outgoing request, and after that I found a solution using HttpEntity (just as in the commented part above):

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 "";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM