简体   繁体   English

通过HTTP发布请求发送键值参数和JSON消息正文

[英]Send key-value parametres and json message body throught http post request

I need to send http POST request from mobile android application to the server side applcation. 我需要从移动Android应用程序向服务器端应用程序发送http POST请求。 This request need to contain json message in body and some key-value parametres. 此请求需要在正文中包含json消息和一些键值参数。 I am try to write this method: 我试图写这种方法:

 public static String makePostRequest(String url, String body,  BasicHttpParams params) throws ClientProtocolException, IOException {
        Logger.i(HttpClientAndroid.class, "Make post request");
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(body);
        httpPost.setParams(params);
        httpPost.setEntity(entity);
        HttpResponse response = getHttpClient().execute(httpPost);
        return handleResponse(response);
    }

Here i set parametres to request throught method setParams and set json body throught setEntity. 在这里,我设置参数以请求通过setParams方法,并通过setEntity设置json正文。 But it isn't work. 但这是行不通的。 Can anybody help to me? 有人可以帮我吗?

You can use a NameValuePair to do this.......... 您可以使用NameValuePair来执行此操作。

Below is the code from my project where I used NameValuePair to sent the xml data and receive the xml response, this will provide u some idea about how to use it with JSON. 以下是我的项目中使用NameValuePair发送xml数据并接收xml响应的代码,这将为您提供有关如何将其与JSON一起使用的一些想法。

public String postData(String url, String xmlQuery) {



    final String urlStr = url;
    final String xmlStr = xmlQuery;
    final StringBuilder sb  = new StringBuilder();


    Thread t1 = new Thread(new Runnable() {

        public void run() {

            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(urlStr);


            try {

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        1);
                nameValuePairs.add(new BasicNameValuePair("xml", xmlStr));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpclient.execute(httppost);

                Log.d("Vivek", response.toString());

                HttpEntity entity = response.getEntity();
                InputStream i = entity.getContent();

                Log.d("Vivek", i.toString());
                InputStreamReader isr = new InputStreamReader(i);

                BufferedReader br = new BufferedReader(isr);

                String s = null;


                while ((s = br.readLine()) != null) {

                    Log.d("YumZing", s);
                    sb.append(s);
                }


                Log.d("Check Now",sb+"");




            } catch (ClientProtocolException e) {

                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } 
        }

    });

    t1.start();
    try {
        t1.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    System.out.println("Getting from Post Data Method "+sb.toString());

    return sb.toString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要执行基本身份验证以及将密钥值作为 Http 发布请求的主体发送并获取数据 - I need to perform Basic authentication as well as send key value as body for a Http Post request and get data 在春季启动时,请求json主体中存在可变键值时处理RequestBody - Handling a RequestBody when there are variable key-value in the request json body in spring boot 从 Camel 中的 multipart/form-data HTTP POST 请求中获取键值对 - Get key-value pairs from multipart/form-data HTTP POST request in Camel 从Restlets中的POST正文获取键值对 - Get key-value pairs from POST body in Restlets 如何使用 JSON 主体在 java 中发送发布请求 - How to send post request in java with a JSON body 使用基本身份验证和请求正文以Java发送HTTP POST请求 - Send HTTP POST request in Java with Basic Auth and request body 如何在restAssured的请求正文中发送带有列表的JSON post请求 - How to send a JSON post request with a list in request body in restAssured 如何在HTTP post请求的消息体中附加XML文件? - How to attach XML file in message body of HTTP post request? 使用给定的请求正文和标头发送 HTTP 帖子并获得响应 - Send HTTP post with the given Request body and headers and get the respone 如何发送带有 json 正文和 url 参数的 http 帖子? - how to send http post with json body and url params?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM