【问题标题】:fail to send two parameters in http - post call未能在 http 中发送两个参数 - post call
【发布时间】:2011-12-25 14:10:00
【问题描述】:

我试图向某个服务器发送两个参数。 服务器正在响应 http-post 调用,两个参数是 诠释 一些枚举(我作为字符串发送)

我想将参数作为json发送:

        StringEntity t = new StringEntity("{ \"intValParam\":-100 , \"enumParam\":\"enumValueAsString\" }" , "UTF-8");

        httppost.setEntity(t);

        httppost.setHeader("content-type", "application/json");

我得到的响应是 400(错误请求)

** 我还可以调用另一种方法,它需要一个参数......只有 int - 而且这个方法工作得很好 - 所以这不是连接不良或类似情况的问题。

【问题讨论】:

    标签: android


    【解决方案1】:

    您不应该尝试这样添加参数。要么使用 httpPost 中的 setParams 方法,要么使用 NameValuePair 实体并将它们编码到您的请求中,就像这样:

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("userid", "12312"));
    nameValuePairs.add(new BasicNameValuePair("sessionid", "234")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    

    代码采用here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2021-07-27
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      相关资源
      最近更新 更多