http请求

public  boolean getSeasonIngredientList() throws ParseException, IOException {

            HttpClient client = new DefaultHttpClient();

            Map<String, String> paramMap = new HashMap<String, String>();

            DouguoRequestParameter parameter= new DouguoRequestParameter();

            String url = ClientConstant. DOUGUONEW_API_URL;

             parameter.setMethod( DOUGUONEW_RECIPE_SEASONSHICAI);//参数一

             parameter.setApp_key( DOUGUONEW_APP_KEY);//参数二

             this.makeSign( parameter);

            String sign = parameter.getSign(); //参数三

            

          String a = "method=" + "douguo.recipe.seasonshicai" + "&app_key=" "98926612" + "&sign=" + sign ;

        HttpGet post = new HttpGet( url+ "?"+ a);  //HttpGet

       

        /**

         * 以下为HttpPost方式请求 -----①将参数封装成一个对象存入post中,进行请求

         * ②用list来存放参数及其参数所对应的值,最后将post.setEntity()将存好参数的list存入post中,进行请求。

         */

//        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); 

//        nvps.add(new BasicNameValuePair("method", "douguo.recipe.seasonshicai")); 

//        nvps.add(new BasicNameValuePair("app_key", "98926612"));

//        nvps.add(new BasicNameValuePair("sign", sign));

//        parameter.setSign(sign);

       

       

        //StringEntity se = new StringEntity(a, "UTF-8");

//        post.setEntity(se);

//        post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

            HttpResponse response = client.execute( post);

          BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();

        String line = "";

        while ((line = rd.readLine()) != null) {

            result.append( line);

        }

        System. out.println( result.toString());

          return false;

      }


注释部分为用map方式存放参数以其对应参数值,来达到封装post的请求。-------------------最后选择HttpGet/HttpPost方式


protected static final String uploadPath = "/upload";


      HttpClient client = new DefaultHttpClient();


      protected static final String postPath = "/api/send";

      

      String appMasterSecret = "wlkzu5wcstiwipag7nqzs1wn8ik9ooau";

      

      public boolean send(UmengRequestPushInfo pushInfo) throws Exception {

        String url = ClientConstant. UMENG_API_URL + postPath ;

        String payload = GsonUtil. toJson(pushInfo);       

        String str = "POST" + url + payload + appMasterSecret;

        String sign = DigestUtils. md5Hex((str).getBytes("utf8"));

        url = url + "?sign=" + sign;

        HttpPost post = new HttpPost( url);

        StringEntity se = new StringEntity( payload, "UTF-8");

        post.setEntity(se);

        // Send the post request and get the response

        HttpResponse response = client.execute( post);

        int status = response.getStatusLine().getStatusCode();

        System. out.println( "Response Code : " + status );

        BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();

        String line = "";

        while ((line = rd.readLine()) != null) {

            result.append( line);

        }

        System. out.println( result.toString());

        if (status == 200) {

            System. out.println( "Notification sent successfully.");

        } else {

            System. out.println( "Failed to send the notification!");

        }

        return true;

    }


      

      public String uploadContents(String appkey,String appMasterSecret,String contents ) throws Exception {

             // Construct the json string

            JSONObject uploadJson = new JSONObject();

             uploadJson.put( "appkey", appkey);

            String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));

             uploadJson.put( "timestamp", timestamp);

             uploadJson.put( "content", contents);

             // Construct the request

            String url = ClientConstant. UMENG_API_URL + uploadPath ;

            String postBody = uploadJson.toString();

            String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(( "POST" + url + postBody + appMasterSecret).getBytes( "utf8"));

             url = url + "?sign=" + sign;

            HttpPost post = new HttpPost( url);

            StringEntity se = new StringEntity( postBody, "UTF-8");

             post.setEntity( se);

             // Send the post request and get the response

            HttpResponse response = client.execute( post);

            System. out.println( "Response Code : " + response.getStatusLine().getStatusCode());

            BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();

            String line = "";

             while (( line = rd.readLine()) != null) {

                   result.append( line);

            }

            System. out.println( result.toString());

             // Decode response string and get file_id from it

            JSONObject respJson = new JSONObject();

            String ret = respJson.getString( "ret");

             if (! ret.equals( "SUCCESS")) {

                   throw new Exception( "Failed to upload file");

            }

            JSONObject data = respJson.getJSONObject( "data");

            String fileId = data.getString( "file_id");

            

             return fileId;

      }


转载于:https://my.oschina.net/Denniswang/blog/661935

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值