【问题标题】:POST Request is invoking doGet methodPOST 请求正在调用 doGet 方法
【发布时间】:2012-03-10 10:45:56
【问题描述】:

我正在将 Post 请求发送到一个 Tomcat Servlet,我很快就一起翻遍了该 Servlet。当我在 Android 上发出 HttpPost 请求时,我看到我在 servlet 中看到了该请求,但那是因为调用了 doGet 方法。谁能向我解释为什么会发生这种情况以及我如何解决它以调用 doPost 方法?

这是发出 post 请求的服务方法:

@Override
protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "handling intent");
    // get Longitude and Latitude
    Bundle bundle = intent.getExtras();
    
    double longitude = bundle.getDouble("longitude");
    double latitude  = bundle.getDouble("latitude");
    // int  id = bundle.getInt("id");
    long time = System.currentTimeMillis();
    
    // log location in local db
    
    // send location up to db repository (repositories)
    JSONObject jObj = new JSONObject();
    try {
        jObj.put("long", longitude);
        jObj.put("lat", latitude);
        jObj.put("userId", 1);
        jObj.put("time", time);
        
        Log.i(TAG, "JSON object: " + jObj.toString());
        
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    try {
        StringEntity se = new StringEntity("JSON" + jObj.toString());
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        
        String url = [http://url/to/servlet/here];
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setEntity(se);
        HttpResponse response;
        
        response = httpClient.execute(post);
        
        // check response
        if (response != null) {
            Log.i(TAG, "Message received");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

以及接收请求的servlet:

public class ReceiveLocation extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Post request received");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        try {
            StringBuilder sb = new StringBuilder();
            String s;
            while ((s = request.getReader().readLine()) != null) {
                sb.append(s);
            }
            System.out.println("sb: " + sb.toString());
       } catch (Exception e) {
           e.printStackTrace();
       }
    }


/**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws         ServletException, IOException {
        processRequest(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Get request received!!!");
    }
}

输出是“收到请求!!!”

编辑

我暂时更改了 doGet 方法,以便跟踪一些我认为可能很重要的事情(我对这一切都很陌生,如果我发布的内容对这种情况没有帮助,请告诉我) .

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("Get request received!!!");
    Enumeration<String> enumerator = request.getHeaderNames();
    while (enumerator.hasMoreElements()) {
        System.out.println("header: " + enumerator.nextElement());
    }

    System.out.println("Method request: " + request.getMethod().toString());
    Enumeration<String> attrEnumerator = request.getAttributeNames();
    while (attrEnumerator.hasMoreElements()) {
        System.out.println("attr:" + attrEnumerator.nextElement());
    }
    Enumeration<String> paramEnumerator = request.getParameterNames();
    while (paramEnumerator.hasMoreElements()) {
        System.out.println("param:" + paramEnumerator.nextElement());
    }
}

输出:

收到请求!!!

标题:主机

标题:连接

标题:用户代理

方法请求:GET

Output from Android:
 I TreasureHuntActivity: Provider network has been selected.
 I TreasureHuntActivity: Init Lat: 30280
 I TreasureHuntActivity: Init Long: -97744
 I SendLocationIntentService: handling intent
 I SendLocationIntentService: JSON object: {"long":0,"time":1329792722150,"lat":0}
 I SendLocationIntentService: Method POST
 I SendLocationIntentService: Entity java.io.ByteArrayInputStream@4058d760
 I SendLocationIntentService: handling intent
 I SendLocationIntentService: JSON object: {"long":0,"time":1329792743161,"lat":0}
 I SendLocationIntentService: Method POST
 I SendLocationIntentService: Entity java.io.ByteArrayInputStream@40594050

【问题讨论】:

  • 您确定您正在运行您认为正在运行的代码吗?
  • 我也遇到过同样的情况。你有想过吗?
  • 我发现了我的问题。我在我的 servlet 的 URL 上省略了最后的正斜杠。 url/to/servlet/here 而不是 url/to/servlet/here

标签: java android http servlets post


【解决方案1】:

您没有使用您的代码进行 http POST,因为您的实体是空的。

Here's 在 Android 中执行 http POST 的方法之一的示例

【讨论】:

  • 你在哪里看到实体是空的?不管是否为空,OP 使用的是HttpPost,而不是HttpGet
  • 您是否尝试将 json 文件/对象作为附件附加并通过 POST 发送?还是您只是将 json 文件/对象的内容传递过来?您是否尝试打印出您的 servlet 接收到的内容?
  • 我正在尝试使用 json 对象传递数据。我对 servlet 编程有点陌生,但我会看看我能找到关于发送的内容。
  • 我很确定“HttpPost post = new HttpPost(url);”这一行创建一个 POST 请求。 @user766495 - 也许检查一下你的 apache/tomcat 中是否有特殊规则?只是猜测......
  • 您对在tomcat 中查看的位置有什么建议吗?我在 context.xml、web.xml、server.xml、catalina.properties、catalina.policy 中进行了快速搜索,但没有找到任何看起来有用的东西(搜索“GET”)
猜你喜欢
  • 2012-01-31
  • 2015-10-02
  • 2022-06-25
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多