Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST Request is invoking doGet method

I'm sending Post requests to a Tomcat Servlet that I quickly rummaged together. When I make an HttpPost request on Android, I see that I see the request in the servlet, but that's because the doGet method is called. Can anyone explain to me why this is happening and how I fix it to call the doPost method?

Here is the service method making the post request:

@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();
    }

}

And the servlet receiving the request:

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

Output is "Get request received!!!"

EDIT

I've temporarily changed the doGet method to give me a trace on a few things I thought might be important (I'm new to all this, please let me know if what I posted doesn't help with this situation).

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

Output:

Get request received!!!

header: host

header: connection

header: user-agent

Method request: 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
like image 930
user766495 Avatar asked Mar 01 '26 02:03

user766495


1 Answers

You're not doing a http POST with your codes, because your entity is empty.

Here's an example of one of the ways to do a http POST in Android

like image 142
Zerhinne Avatar answered Mar 02 '26 16:03

Zerhinne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!