您的位置:首页 > 理论基础 > 计算机网络

http post 请求参数格式是json

2017-08-31 18:15 761 查看
/****

    通过主方法调用oneTest类中的方法测试

    1.入参我们是把map通过Gson转成json字符串

****/

  public static void main(String []args){
    Map<String,String> params =new HashMap<String,String>();
    params.put("ua", "Mozilla/5.0 (Linux; Android 4.4.2; Lingwin K7 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36");
    params.put("ip", "112.64.127.226");
    params.put("width", "720");
    params.put("height", "1280");
   
    Gson gson = new Gson();
    String json = gson.toJson(params);
    oneCloudADAPITV ad=new oneCloudADAPITV();
        ad.AdvertisementInfo(json);
    System.out.println("-------------");
     
   }

/***

   1.通过httppost请求URL 得到JSON字符串

 ***/

   

import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

public class oneTest {

 public static final String DEF_CHATSET = "UTF-8";
   public static final int DEF_CONN_TIMEOUT = 30000;
   public static final int DEF_READ_TIMEOUT = 30000;
   public  static String userAgent =  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
   //广告
   private static String Url_Advertisement="";
   private static String Url_Advertisement_Test="";
      

/**
  * 
  * @param pa
4000
rams 请求参数
  * @return  网络请求字符串
 
  * @throws Exception 

       * 执行一个HTTP POST请求,返回请求响应的HTML  

       *  

       * @return 返回请求响应的HTML  

       */   

      public static String AdvertisementInfo(String json) {   //Map<String,String> params

           String url=Url_Advertisement;

         String result ="";    

         DefaultHttpClient client = new DefaultHttpClient();  

            HttpPost post = new HttpPost(url);  

            try {  

              StringEntity s = new StringEntity(json);  

              s.setContentEncoding("UTF-8");  

              s.setContentType("application/json");       //发送json数据需要设置contentType  

              post.setEntity(s);  

              HttpResponse res = client.execute(post);  

              if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  

                result = EntityUtils.toString(res.getEntity());// 返回json格式:  

              }  

            } catch (Exception e) {  

              throw new RuntimeException(e);  

            } 

            return result;

      }   

       

      

      /**
  * 
  * @param params 请求参数
  * @return  网络请求字符串
 
  * @throws Exception 

       * 执行一个HTTP POST请求,返回请求响应的String  

       *  

       * @return 返回请求响应的String  

       */   

      public static String AdvertisementInfoTest(String json) {   //Map<String,String> params

         String url=Url_Advertisement_Test;

         String result ="";    

         DefaultHttpClient client = new DefaultHttpClient();  

            HttpPost post = new HttpPost(url);  

            try {  

              StringEntity s = new StringEntity(json);  

              s.setContentEncoding("UTF-8");  

              s.setContentType("application/json");       //发送json数据需要设置contentType  

              post.setEntity(s);  

              HttpResponse res = client.execute(post);  

              if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  

                result = EntityUtils.toString(res.getEntity());// 返回json格式:  

              }  

            } catch (Exception e) {  

              throw new RuntimeException(e);  

            } 

            return result;

      }   
}

     /**

    1.把JSON字符串转成map

    2.通过KEY 取出你要的值封装成你要的json

  ***/

                                                               Map json = (Map) JSON.parse(result.getBody());
for (Object map : json.entrySet()) {
if (("alipay_trade_query_response").equals(((Map.Entry) map).getKey().toString())) {
Map jsonbody = (Map) JSON.parse(((Map.Entry) map).getValue().toString());


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: