【问题标题】:Android Post to API Doesn't Work, Ajax Post Works OK. Required String parameter 'firstname' is not present"Android Post to API 不工作,Ajax Post 工作正常。所需的字符串参数“名字”不存在”
【发布时间】:2016-11-17 15:50:08
【问题描述】:

我有一个 Spring Framework API 设置来接收 GET/POST 请求,该请求在网络上运行良好,但是当我尝试从 Android 发布时,我得到一个“必需的字符串参数 'firstname' 不存在”。

我尝试使用不同的迭代发布,但不断收到相同的错误。

Java Spring 框架

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@RequestParam(value="firstname") String firstname,
                      @RequestParam(value="lastname") String lastname,
                      @RequestParam(value="username") String username,
                      @RequestParam(value="password") String password) throws Exception {
    String success = add.addUser(firstname, lastname, username, password);
    return toJson(success);
}

Javascript 版本

var data = {
        firstname: firstname,
        lastname: lastname,
        username : username,
        password : password
    };
    $.ajax({
        type: 'POST',
        url: "<url removed>/import/addUser",
        success: function(data) {
            callbackSuccess(JSON.stringify(data));
        },
        error: function(data) {
            callbackFail(data);
        },
        data: data,
        dataType: "json"
    });

安卓

        String url = "<url removed>/import/addUser";
        InputStream inputStream = null;
        String result = "";
                try {
                    // 1. create HttpClient
                    HttpClient httpclient = new DefaultHttpClient();

                    // 2. make POST request to the given URL
                    HttpPost httpPost = new HttpPost(url);
                    String json = "";
                    // 3. build jsonObject
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.accumulate("firstname", userFirstName);
                    jsonObject.accumulate("lastname", userLastName);
                    jsonObject.accumulate("username", emailStr);
                    jsonObject.accumulate("password", passwordStr);

                    // 4. convert JSONObject to JSON to String
                    json = jsonObject.toString();

                    // 5. set json to StringEntity
                    StringEntity se = new StringEntity(json);

                    // 6. set httpPost Entity
                    httpPost.setEntity(se);
                    Log.i("Sending", json);
                    // 7. Set some headers to inform server about the type of the content
                    httpPost.setHeader("Accept", "application/json");
                    httpPost.setHeader("Content-type", "application/json");

                    // 8. Execute POST request to the given URL
                    HttpResponse httpResponse = httpclient.execute(httpPost);

                    // 9. receive response as inputStream
                    inputStream = httpResponse.getEntity().getContent();

                    // 10. convert inputstream to string
                    if(inputStream != null)
                        result = convertInputStreamToString(inputStream);
                    else
                        result = "Did not work!";

                Log.i("Response", result);

有什么想法或建议吗?

更新 我已经修改了 Spring Framework 代码,因此不需要参数并且我没有收到任何值。只有 null 作为变量。所以看起来确实是 POST 本身而不是服务器端存在问题。

【问题讨论】:

  • 你试过@RequestParam(value="firstname" , required = false) String firstname 吗?

标签: javascript java android ajax spring


【解决方案1】:

内容类型应为application/x-www-form-urlencoded。看到这个SO它曾经帮助过我一次

【讨论】:

  • 嗯!让我们试试别的吧!在 se se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 上设置 content-type
猜你喜欢
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
相关资源
最近更新 更多