AsyncHttpClient POST params become null on server

283 views Asked by At

I am writing method to insert a new record to DB from android. On client (android studio), I use AsyncHttpClient POST:

    JSONObject params = new JSONObject();
    try {
        params.put("idOrd", idOrd);
        params.put("idLan", aIdLan);
        params.put("dbIP", dbIP);
        params.put("dbName", dbName);
        params.put("dbUsername", dbUsername);
        params.put("dbPassword", Utility.dbEncrypt(dbPassword));
        wsEditMaster(params);
    } catch (JSONException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }

public void wsEditMaster(final JSONObject params) throws UnsupportedEncodingException {
        ByteArrayEntity entity = new ByteArrayEntity(params.toString().getBytes("UTF-8"));
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"));
        client.post(this, "http://" + serverIP + "/DHD/general/editorder", entity, "application/x-www-form-urlencoded", new AsyncHttpResponseHandler() {

And on server (eclipse):

// HTTP Post Method
@POST
// Path: http://localhost/<appln-folder-name>/general/editorder
@Path("/editorder")
// Produces JSON as response
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
// Query parameters are parameters:
public String editOrder(@FormParam("idOrd") String idOrd,
        @FormParam("idLan") String idLan, @FormParam("dbIP") String dbIP,
        @FormParam("dbName") String dbName,
        @FormParam("dbUsername") String dbUsername,
        @FormParam("dbPassword") String dbPassword) throws Exception {
    String response = "";
    if (DBConnection.editOrder(idOrd, idLan, dbIP, dbName, dbUsername, dbPassword)) {
        response = Utility.constructJSON("editOrder", true);
    } else {
        response = Utility.constructJSON("editOrder", false,
                "Cannot insert to database!");
    }
    return response;
}

Everything works fine when I use GET, but when I use POST, all params became null in "editOrder" function. Please help, thank you.

1

There are 1 answers

0
Jacky On

OK, I solved my problem. Simply, use RequestParams instead of JSONObject:

public void wsEditMaster(final RequestParams params) {
        client.post("http://" + serverIP + "/DHD/general/editorder", params, new AsyncHttpResponseHandler() {