无法在http - post call中发送两个参数

时间:2011-12-25 14:10:00

标签: android

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

我想将参数发送为json:

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

        httppost.setEntity(t);

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

我得到的回复是400(错误请求)

**还有一个我可以调用的方法,需要有一个参数...只有int - 并且这个方法运行良好 - 所以这不是来自错误连接或类似的问题。

1 个答案:

答案 0 :(得分: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

相关问题