如何使用参数,HttpPost [英] How to use parameters with HttpPost

查看:137
本文介绍了如何使用参数,HttpPost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个梅索德一个REST风格的web服务:

  @POST
@Consumes({应用/ JSON})
@Path(创建/)
公共无效创建(字符串STR1,字符串STR2){
的System.out.println(值1 =+ STR1);
的System.out.println(值2 =+ STR2);
}
 

在我的Andr​​oid应用我想调用此方法。我怎么给使用org.apache.http.client.methods.HttpPost的正确值的参数;

我注意到,我可以使用注释@HeaderParam,只是添加页眉到HttpPost对象。这是正确的方法是什么?做这样的:

  httpPost.setHeader(接受,应用/ JSON);
httpPost.setHeader(STR1,值);
httpPost.setHeader(STR2,其他值);
 

使用上httpPost的setEntity梅索德将无法正常工作。它只设置参数STR1用JSON字符串。当使用它像:

 的JSONObject JSON =新的JSONObject();
json.put(STR1,值);
json.put(STR2,其他值);
HttpEntity E =新StringEntity(json.toString());
httpPost.setEntity(E);
//服务器输出:值1 = {STR1:值,str2的:另一个值}
 

解决方案

要设置PARAMS您HttpPostRequest您可以使用 BasicNameValuePair <​​/ code>,是这样的:

  HttpClient的HttpClient的;
    HttpPost httppost;
    ArrayList的&LT;的NameValuePair&GT; postParameters;
    HttpClient的=新DefaultHttpClient();
    httppost =新HttpPost(你的登录链接);


    postParameters =新的ArrayList&LT;的NameValuePair&GT;();
    postParameters.add(新BasicNameValuePair(参数1,param1_value));
    postParameters.add(新BasicNameValuePair(param2的,param2_value));

    httppost.setEntity(新UrlEn codedFormEntity(postParameters));

    HTT presponse响应= httpclient.execute(httppost);
 

I am using a RESTfull webservice with this methode:

@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);
}

In my Android app I want to call this method. How do I give the correct values to the parameters using org.apache.http.client.methods.HttpPost;

I have noticed that I can use the annotation @HeaderParam and simply add headers to the HttpPost object. Is this the correct way? Doing it like:

httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("str1", "a value");
httpPost.setHeader("str2", "another value");

Using the setEntity methode on httpPost won't work. It only sets the parameter str1 with the json string. When using it like:

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"} 

解决方案

To set params to your HttpPostRequest you can use BasicNameValuePair, something like this :

    HttpClient httpclient;
    HttpPost httppost;
    ArrayList<NameValuePair> postParameters;
    httpclient = new DefaultHttpClient();
    httppost = new HttpPost("your login link");


    postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("param1", "param1_value"));
    postParameters.add(new BasicNameValuePair("param2", "param2_value"));

    httppost.setEntity(new UrlEncodedFormEntity(postParameters));

    HttpResponse response = httpclient.execute(httppost);

这篇关于如何使用参数,HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆