0

I'm trying to post XML to my Tomcat server from an Android device, but I get zero bytes in the stream. I get a 400 bad request response, but I am not sure what's wrong with it. I can access my web application in a browser from Android and on my server's machine. When I log the XML, it looks fine. Why am I getting a 400: bad request?

[Update 4]: I just intercepted the request with Wireshark and get (Len=0) Length of zero. I still don't know why though.

[Update 3]: Posting to 10.0.2.2 crashes with connection to http://10.0.2.2:8080/Archery refused. Posting to my server's actual IP address gets the bad request.

[Update 2]: logcat

...
    08-06 15:32:39.189 W/myApp   (2064): xstream created
    08-06 15:32:39.269 W/myApp   (2064): xml created
    08-06 15:32:39.269 E/test    (2064): <html><head></head><body>Why don't you work?</body></html>
    08-06 15:32:39.269 E/test    (2064): XML set to String Entity
    08-06 15:32:39.269 E/test    (2064): post headers set
    08-06 15:32:39.269 E/test    (2064): Sending post
    08-06 15:32:39.319 W/InputManagerService(239): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@2bc4a7c0
    08-06 15:32:39.339 E/test    (2064): Post sent, response is: HTTP/1.1 400 Bad Request[Lorg.apache.http.Header;@2c0695d0org.apache.http.impl.client.ClientParamsStack@2c1a4cd0
    08-06 15:32:41.309 D/OpenGLRenderer(337): Flushing caches (mode 0)
    08-06 15:32:41.409 W/InputManagerService(239): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@2bc79268
    08-06 15:32:41.589 I/ActivityManager(239): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.cyanogenmod.trebuchet/.Launcher} from pid 239
...

[Update 1]: When my request is like this:

StringEntity se = new StringEntity(xmlString, "UTF-8");
Log.e("test", "XML set to String Entity");
se.setContentType("text/xml");
post.setEntity(se);
post.setHeader("Content-Type", "text/xml; charset=utf-8");
post.setHeader("Host", "http://xxx.xxx.x.x:8080/Archery");
Log.e("test", "post headers set");

I get this logged when using RequestDumpFilter:

INFO: Request Received at 2013-08-06 13:27:01.342
 characterEncoding=null
     contentLength=-1
       contentType=null
            locale=en_US
           locales=en_US, en
          protocol=HTTP/1.1
        remoteAddr=0:0:0:0:0:0:0:1
        remoteHost=0:0:0:0:0:0:0:1
            scheme=http
        serverName=localhost
        serverPort=8080
          isSecure=false
---------------------------------------------
       contextPath=
            header=host=localhost:8080
            header=user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0) Gecko/20100101 Firefox/22.0
            header=accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
            header=accept-language=en-US,en;q=0.5
            header=accept-encoding=gzip, deflate
            header=connection=keep-alive
            header=cache-control=max-age=0
            method=GET
          pathInfo=null
       queryString=null
        remoteUser=null
requestedSessionId=null
        requestURI=/Archery
       servletPath=/Archery
=============================================

And adding content length header, post.setHeader("Content-Length", Long.toString(se.getContentLength()));, crashes the program with a ClientProtocolException

Server processRequest method:

protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    //String xmlSent = req.toString();

    resp.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = resp.getWriter();

    InputStream xmlStream = req.getInputStream();
    if (xmlStream != null){
        pw.println("XML stream: " + xmlStream.available());
    }
    else{
        pw.println("No XML stream found");
    }
    String xmlSent = convertStreamToString(xmlStream);
    ShootRecord recordSent = XMLTransformer.xmlToObject(xmlSent);

    if(recordSent!=null){
        String date =  recordSent.listOfScoreSheets.get(0).getmDate();
        String target = recordSent.listOfScoreSheets.get(0).getmTargetNumber();
        String dateTargetKey = date + target;
        shootRecordMap.put(dateTargetKey, recordSent);
    }

    //out.write(xmlSent.getBytes());
    if (recordSent != null){
        pw.println("Date of record sent: "+recordSent.shootRecordDate);
    }
    else{
        pw.println("No record received yet");
    }
    pw.flush();
    pw.close();
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. doGet and doPost call processRequest">
// </editor-fold>

postToServletOverWifi method

public boolean postToServletOverWifi(ShootRecord recToSend, Context context)
{
    try
    {
        Log.w("myApp", "starting method postToServletOverWifi");

        /* Prepare object as XML */
        XStream xstream = new XStream();
        xstream.processAnnotations(ScoreSheet.class);
        xstream.processAnnotations(ShootRecord.class);
        Log.w("myApp", "xstream created");

        ArrayList<ScoreSheet> mListOfScoreSheets = recToSend.getListOfScoreSheets();
        ShootRecord testShootRecord = new ShootRecord();
        testShootRecord.setmShootRecordDate(recToSend.getRecordDate());
        testShootRecord.setmNumArchersOnTarget(recToSend.getNumArchersOnTarget());
        testShootRecord.setmScoreSheetTypeIndex(recToSend.getScoreSheetType());
        testShootRecord.setmShootRecordName(recToSend.getmShootRecordName());
        testShootRecord.setmListOfScoreSheets(mListOfScoreSheets);

        String xml = xstream.toXML(testShootRecord);
        Log.w("myApp", "XML created");

        new taskForPostingToServer(xml).execute();

        // p.flush();
        // ostream.close();

        return true;
    }
    catch (Exception e) {
        System.out.println( e.getMessage() );
        e.printStackTrace();
        return false;
    }
}

Android Task

public taskForPostingToServer(String xml)
{
    xmlString = xml;
    client = new DefaultHttpClient();
    url = "http://xxx.xxx.x.x:8080/Archery";
    post = new HttpPost(url);
    //post.setHeader("Content-type", "text/xml; charset=utf-8");
    post.setHeader("host", "http://xxx.xxx.x.x:8080/Archery");
    response = null;
}

protected void onPreExecute()
{
    //StringEntity se = null;
    try {
        //se = new StringEntity(xmlString, "UTF-8");
        //post.setEntity(se);
        Log.e("test", xmlString);
        InputStream inputStream=new ByteArrayInputStream(xmlString.getBytes()); //Init your own inputstream
        InputStreamEntity inputStreamEntity=new InputStreamEntity(inputStream, xmlString.getBytes().length);
        post.setEntity(inputStreamEntity);
        post.setHeader("Content-Type", "text/xml;charset=UTF-8");
    }
    catch (Exception ex) {
        Logger.getLogger(ScoreSheetsView.class.getName()).log(Level.SEVERE, null, ex);
    }
}


protected Boolean doInBackground(Integer... params)
{
    boolean success = true;
    try {
        Log.e("test", "Sending post");
        response = client.execute(post);
        Log.e("test", "Post sent, "+ "response is: "+response.getStatusLine());

    }
    catch (IOException ex) {
        Logger.getLogger(ScoreSheetsView.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (response == null){
        success =false;
    }
    return success;
}
...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jsky
  • 2,225
  • 5
  • 38
  • 54
  • possible mismatching data Type – Arash GM Aug 03 '13 at 11:44
  • im getting 0 bytes `xmlStream.available()` before i try to convert it back.. – jsky Aug 03 '13 at 11:56
  • logcat added, please vote my question back up? – jsky Aug 06 '13 at 05:44
  • I see you are trying do a POST request and I see 'method=GET' in request dump. – Grzegorz Żur Aug 06 '13 at 08:18
  • i only get the filter caught messages in the output when i access my servlet's url. im unsure how to configure it to log to a file using my maven/embedded tomcat setup, but the filter is supposed to catch all requests. when i make a request from my client, such a caught message doesnt showup like it does from browser access.. – jsky Aug 06 '13 at 08:32
  • ie. i guess i am unsure if it is / or how to make it catch POST requests and it only seems to be logging GET's – jsky Aug 06 '13 at 08:35

0 Answers0