Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make http post from android to google app engine server?

I am trying to make an http post request from my android to my server on GAE.

On the android side it seems like the post goes fine, but on the server side, doPost(...) never gets called. Can anyone tell me why?

Here's my relevant android code:

protected Integer doInBackground(View... arg0)
{
    try
    {
        HttpClient httpClient = new DefaultHttpClient();
//      HttpPost httpPost = new HttpPost("http://elf-security.appspot.com");
        HttpPost httpPost = new HttpPost("http://elf-security.appspot.com/gae_hellowordl");
//      HttpPost httpPost = new HttpPost("http://localhost:8888/gae_hellowordl");
        httpPost.addHeader("test", "Success!");

        Log.i(TAG, "Just about to send http request to " + httpPost.getURI());
        HttpResponse httpResponse = httpClient.execute(httpPost);
        Log.i(TAG, "Received http response..");

        Log.i(TAG, httpResponse.toString());
    } catch (ClientProtocolException e) 
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }

    return null;
}

And here's my relevant server code:

public class GAE_HelloWordlServlet extends HttpServlet
{
    private Logger LOG;

    public GAE_HelloWordlServlet()
    {
        LOG = Logger.getLogger("GAE_HelloWordlServlet");
        LOG.info("GAE_HelloWordlServlet()");
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
        LOG.info("doGet():  request test header:  " + req.getHeader("test"));
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
        LOG.info("doPost():  request test header:  " + req.getHeader("test"));
    }
}

Thanks in advance!


EDIT

Here is my web.xml file:

<?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>GAE_HelloWordl</servlet-name>
        <servlet-class>gae.helloworld.GAE_HelloWordlServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GAE_HelloWordl</servlet-name>
        <url-pattern>/*</url-pattern>
<!--        <url-pattern>/gae_hellowordl</url-pattern> -->
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>SystemServiceServlet</servlet-name>
        <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
        <init-param>
            <param-name>services</param-name>
            <param-value/>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>SystemServiceServlet</servlet-name>
        <url-pattern>/_ah/spi/*</url-pattern>
    </servlet-mapping>
</web-app>
like image 257
Felix Avatar asked Feb 21 '13 01:02

Felix


Video Answer


1 Answers

Double check your web.xml and make sure the URL path is properly mapped to the servlet that handle it:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>helloworld</servlet-name>
        <servlet-class>com.mysite.GAE_HelloWordlServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Check out dev guide here for more details:

Servlets and URL Paths

web.xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method (e.g. the doGet() method for HTTP GET requests).

... ...

Update:

  • Case 1: Android emulator -> Appengine dev_server (both running on same machine):

    See this Q&A: Android Application doPost

  • Case 2: Android real device -> Appengine dev_server (both on the same network):

    First make sure Appengine dev_appserver is accessible on the network (by starting dev_appserver with argument --address=0.0.0.0, see here for more details). Second in your android app, change your code to point to the correct IP address, say if your desktop IP is 192.168.1.6, use HttpPost httpPost = new HttpPost("http://192.168.1.6:8888/gae_hellowordl");

like image 93
yorkw Avatar answered Oct 13 '22 05:10

yorkw