AsyncHttpClient POST参数在服务器上变为null

时间:2016-12-22 04:57:05

标签: android post asynchttpclient

我正在编写方法从android向DB插入新记录。 在客户端(android studio)上,我使用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() {

在服务器上(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;
}

当我使用GET时,一切正常,但是当我使用POST时,所有参数在&#34; editOrder&#34;功能。 请帮忙,谢谢。

1 个答案:

答案 0 :(得分:0)

好的,我解决了我的问题。简单地说,使用RequestParams而不是JSONObject:

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