相关疑难解决方法(0)

如何使用HttpPost参数

我正在使用这个方法的RESTfull webservice:

@POST
@Consumes({"application/json"})
@Path("create/")
public void create(String str1, String str2){
System.out.println("value 1 = " + str1);
System.out.println("value 2 = " + str2);
}
Run Code Online (Sandbox Code Playgroud)

在我的Android应用中,我想调用此方法.如何使用org.apache.http.client.methods.HttpPost为参数赋予正确的值;

我注意到我可以使用注释@HeaderParam并简单地将标头添加到HttpPost对象.这是正确的方法吗?这样做:

httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("str1", "a value");
httpPost.setHeader("str2", "another value");
Run Code Online (Sandbox Code Playgroud)

在httpPost上使用setEntity方法将不起作用.它仅使用json字符串设置参数str1.使用时如:

JSONObject json = new JSONObject();
json.put("str1", "a value");
json.put("str2", "another value");
HttpEntity e = new StringEntity(json.toString());
httpPost.setEntity(e);
//server output: value 1 = {"str1":"a value","str2":"another value"} 
Run Code Online (Sandbox Code Playgroud)

java android json web-services http-post

54
推荐指数
2
解决办法
16万
查看次数

标签 统计

android ×1

http-post ×1

java ×1

json ×1

web-services ×1