Http post添加数据请求

时间:2014-05-11 17:18:10

标签: android json post http-post

我想发送如下数据:

 subscription = [{
          "category":"news", 
          "content_id","id ",  
          },]

到网址subscribe/?=authparams{"token":"1652d47b7c16da74e5d021510e441f49", “action”:”set”}

我该怎么做?我接下来尝试,但它对我不起作用。

  String xml = "authparams={\"token\":\"" + token + "\",\"action\":\"" + "set"+ "\"}";
        // Creating HTTP client
        String subs="subscription =[{\"category\":\"posts\"," +
                "\"content_id\",\""+id+"\",}]";
        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost("http://site.co/api/subscribe/");
        // Url Encoding the POST parameters
        try {
            httpPost.setHeader("Content-Type",
                    "application/x-www-form-urlencoded");
            httpPost.setEntity(new ByteArrayEntity(xml.getBytes("UTF-8")));
            httpPost.setEntity(new ByteArrayEntity(subs.getBytes("UTF-8")));

        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }
        // Making HTTP Request
        try {
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            Log.d("myLogs:", EntityUtils.toString(entity));
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();

        }

1 个答案:

答案 0 :(得分:0)

List<NameValuePair> submit_data = new ArrayList<NameValuePair>();
submit_data.add(new BasicNameValuePair("category", "news");
submit_data.add(new BasicNameValuePair("content_id", "id");

String response = "";

DefaultHttpClient client = new DefaultHttpClient();
HttpPost http_client = new HttpPost("http://site.co/api/subscribe?authparams[token]=1652d47b7c16da74e5d021510e441f49&authparams[action]=set");
try {
    http_client.setEntity(new UrlEncodedFormEntity(submit_data));
    HttpResponse response = client.execute(http_client);
    HttpEntity entity = response.getEntity();
    Log.d("myLogs:", EntityUtils.toString(entity));

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