android http post请求 restful,如何向RESTful webservice发送HttpPost请求以传递包含值数组参数的URL?…

  • Post author:
  • Post category:其他


我正在尝试调用RESTful webservice来获取JSON对象。现在我试着通过HttpGet进行通话并且成功了。我需要通过的URL非常类似于:http://example.com/ /def.xxx?Name=save &代码=示例& OrderDetails = [{“Count”:“2”,“ID”:“1”,“Price”: “5”}]。我如何向RESTful webservice发送HttpPost请求以传递包含值数组参数的URL?

`

StringBuilder URL = new StringBuilder(“http://example.com/def.xxx?”);

URL.append(“Name=”+name+”&”);

URL.append(“Code=”+code+”&”);

URL.append(“Details=%5b”);

int val = 0;

for (int i = 0; i

if (val > 0)

{URL.append(“,”);

}

else

val = 1;

URL.append(…..);

URLX = URL.toString();

httpGet = new HttpGet(URLX);

response = client1.execute(httpGet);

`

现在,如果我想使HttpPost调用,而不是HTTPGET的叫什么,应该怎么办?我想用这种方式,

String URL = “http://example.com/def.xxx”;

DefaultHttpClient client1 = new DefaultHttpClient();

HttpResponse response = null;

HttpPost httpPost = new HttpPost();

ArrayList postParameters;

postParameters = new ArrayList();

postParameters.add(new BasicNameValuePair(“Name”, name));

postParameters.add(new BasicNameValuePair(“Code”, code));

try {

httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

} catch (UnsupportedEncodingException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

response = client1.execute(httpPost);

} 现在我不知道我应该如何在详细信息中添加值对= [{“计数”:“2”,“ID”:“1”,“价格“:”5“}]在Post调用中,我应该如何执行它以获取与获取HttpGet时所获得的JSON对象相同的对象。请帮忙。

2013-06-24

MSIslam