5

我正在尝试从我的 android 向 GAE 上的服务器发出 http 发布请求。

在 android 端,帖子似乎运行良好,但在服务器端,doPost(...) 永远不会被调用。谁能告诉我为什么?

这是我的相关android代码:

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

这是我的相关服务器代码:

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

提前致谢!


编辑

这是我的 web.xml 文件:

<?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>
4

1 回答 1

5

仔细检查您的 web.xml 并确保 URL 路径正确映射到处理它的 servlet:

<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>

在此处查看开发指南以获取更多详细信息:

Servlet 和 URL 路径

web.xml 定义了 URL 路径和使用这些路径处理请求的 servlet 之间的映射。Web 服务器使用此配置来识别处理给定请求的 servlet 并调用与请求方法对应的类方法(例如,用于 HTTP GET 请求的 doGet() 方法)。

……

更新:

  • 案例 1:Android 模拟器 -> Appengine dev_server(都在同一台机器上运行):

    请参阅此问答:Android 应用程序 doPost

  • 案例2:Android真机->Appengine dev_server(都在同一个网络):

    首先确保 Appengine dev_appserver 可以在网络上访问(通过使用参数 --address=0.0.0.0 启动 dev_appserver,请参阅此处了解更多详细信息)。其次,在您的 android 应用程序中,更改您的代码以指向正确的 IP 地址,例如,如果您的桌面 IP 是 192.168.1.6,请使用HttpPost httpPost = new HttpPost("http://192.168.1.6:8888/gae_hellowordl");

于 2013-02-21T02:36:07.753 回答