Java HttpClient Post多层json格式参数

来源:互联网 发布:openwrt 挂载网络共享 编辑:程序博客网 时间:2024/05/22 14:10
package ...

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpWeiXinMessage {
    public static String WeiXinMessage(String url,Map<String,String> params) throws ClientProtocolException, IOException{
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost =  new HttpPost(url);
        httpPost.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//请求头
        /*方法一:一层json*/
        //StringEntity stringEntity = new StringEntity(data,"UTF-8");
        //httpPost.setEntity(stringEntity);//请求主体
        /*方法二*:多层json*/
        HttpWeiXinMessage.setPostData(httpPost, params);
        
        HttpResponse httpResponse = httpClient.execute(httpPost);//发送请求
        HttpEntity httpEntity = httpResponse.getEntity();//获取请求返回体
        String backResult =  EntityUtils.toString(httpEntity,"UTF-8");//请求返回结果
        
        if(httpResponse != null){
          try{
            EntityUtils.consume(httpResponse.getEntity());
            }catch(IOException e){
                e.printStackTrace();
            }
        }//释放资源
        
        return backResult;
        
    }
    
    //设置请求参数
    private static void setPostData(HttpPost httpPost, Map<String,String> params){
        /*另外一种设置请求参数方法*/
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
        }
        HttpEntity httpEntity = null;
        try {
            httpEntity = new UrlEncodedFormEntity(list, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpPost.setEntity(httpEntity);//设置请求主体
    }
    
    //传递Map<String,Strnig> params参数格式
    Map<String, Strnig> map = new LinkedHashMap<String, String>();
    map.put("contexcode", "log");
    map.put("source", "weixin");
    map.put("form", "WeChat");
    map.put("messageId", "jjtz");
    map.put("data", "{'touser':" + amv.getOpenId() + ",'data':{'first': {'value':'qjtz','color':'#0'}," +
    "'keyword1':{'value':'"+realName+"','color':'#0'},'keyword2': {'value':'"+phone+"','color':'#173177'}," +
    "'remark': {'value':'"+senderAddress+"','color':'#173177'}}}");
}

0 0