1:Util類方法
/** * 發送 Post請求 * * @param url * @param reqXml * @return */ public static String post(String url, List<NameValuePair> params) { String body = null; try { // 設置客戶端編碼 if (httpClient == null) { // Create HttpClient Object httpClient = new DefaultHttpClient(); } // Post請求 HttpPost httppost = new HttpPost(url); // 設置參數 httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); // 發送請求 HttpResponse httpresponse = httpClient.execute(httppost); // 獲取返回數據 HttpEntity entity = httpresponse.getEntity(); body = EntityUtils.toString(entity,"UTF-8"); if (entity != null) { entity.consumeContent(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return body; }
2:調用接口
//調用接口 NameValuePair json = new BasicNameValuePair("json",jsonData); NameValuePair mobile = new BasicNameValuePair("mobile",stallApply.getMobile()); NameValuePair templateCode = new BasicNameValuePair("templateCode","SMS_74665033"); NameValuePair ntimestamp = new BasicNameValuePair("timestamp",timestamp); NameValuePair nsignature = new BasicNameValuePair("signature",signature); String url = orderDomain+"/code/sendMsmMsg"; List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(json); list.add(mobile); list.add(templateCode); list.add(ntimestamp); list.add(nsignature); String data = HttpClientUtil.post(url,list); System.out.println(data);