1、基于 JAVA的加油卡在线充值接口调用代码实例代码描述:基于 JAVA的加油卡在线充值接口调用代码实例代码平台:聚合数据import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import .HttpURLConnection;import .URL;import .U
2、RLEncoder;import java.util.HashMap;import java.util.Map;import net.sf.json.JSONObject;/*加油卡充值调 用示例代码 聚合数据*在 线接口文档:http:/ class JuheDemo 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 Str
3、ing userAgent = “Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36“;/配置您申请的 KEYpublic static final String APPKEY =“*“;/1.订单状态查询public static void getRequest1()String result =null;String url =“http:/ params = new HashMap();/请求参数params.put(“orderid“,
4、“);/商家订单号,8-32 位字母数字组合params.put(“key“,APPKEY);/应用 APPKEY(应用详细页查询)try result =net(url, params, “GET“);JSONObject object = JSONObject.fromObject(result);if(object.getInt(“error_code“)=0)System.out.println(object.get(“result“);elseSystem.out.println(object.get(“error_code“)+“:“+object.get(“reason“); c
5、atch (Exception e) e.printStackTrace();/2.账户余额查询public static void getRequest2()String result =null;String url =“http:/ params = new HashMap();/请求参数params.put(“timestamp“,“);/当前 时间戳,如:1432788379params.put(“key“,APPKEY);/应用 APPKEY(应用详细页查询)params.put(“sign“,“);/校验值,md5(OpenID+key+timestamp),OpenID 在个人
6、中心查询try result =net(url, params, “GET“);JSONObject object = JSONObject.fromObject(result);if(object.getInt(“error_code“)=0)System.out.println(object.get(“result“);elseSystem.out.println(object.get(“error_code“)+“:“+object.get(“reason“); catch (Exception e) e.printStackTrace();/3.加油卡充值public static v
7、oid getRequest3()String result =null;String url =“http:/ params = new HashMap();/请求参数params.put(“proid“,“);/产品 id:10000(中石化 50 元加油卡)、10001(中石化 100 元加油卡)、 10003(中石化 500 元加油卡 )、10004(中石化 1000 元加油卡)、10007(中石化任意金额充值)、10008(中石油任意金额充值)params.put(“cardnum“,“);/充值数量 任意充 (整数(元),其余面值固定值为 1params.put(“orderid“
8、,“);/商家订单号,8-32 位字母数字组合params.put(“game_userid“,“);/加油卡卡号,中石化:以 100011 开头的卡号、中石油:以 9 开头 的卡号params.put(“gasCardTel“,“);/持卡人手机号码params.put(“gasCardName“,“);/持卡人姓名params.put(“chargeType“,“);/加油卡类型 (1:中石化、2: 中石油;默认为 1)params.put(“key“,APPKEY);/应用 APPKEY(应用详细页查询)params.put(“sign“,“);/校验值,md5(OpenID+key+p
9、roid+cardnum+game_userid+orderid),OpenID 在个人中心查询try result =net(url, params, “GET“);JSONObject object = JSONObject.fromObject(result);if(object.getInt(“error_code“)=0)System.out.println(object.get(“result“);elseSystem.out.println(object.get(“error_code“)+“:“+object.get(“reason“); catch (Exception e)
10、 e.printStackTrace();public static void main(String args) /* param strUrl 请求地址* param params 请求参数* param method 请求方法* return 网络请求字符串* throws Exception*/public static String net(String strUrl, Map params,String method) throws Exception HttpURLConnection conn = null;BufferedReader reader = null;String
11、 rs = null;try StringBuffer sb = new StringBuffer();if(method=null | method.equals(“GET“)strUrl = strUrl+“?“+urlencode(params);URL url = new URL(strUrl);conn = (HttpURLConnection) url.openConnection();if(method=null | method.equals(“GET“)conn.setRequestMethod(“GET“);elseconn.setRequestMethod(“POST“)
12、;conn.setDoOutput(true);conn.setRequestProperty(“User-agent“, userAgent);conn.setUseCaches(false);conn.setConnectTimeout(DEF_CONN_TIMEOUT);conn.setReadTimeout(DEF_READ_TIMEOUT);conn.setInstanceFollowRedirects(false);conn.connect();if (params!= null out.writeBytes(urlencode(params); catch (Exception
13、e) e.printStackTrace();InputStream is = conn.getInputStream();reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET);String strRead = null;while (strRead = reader.readLine() != null) sb.append(strRead);rs = sb.toString(); catch (IOException e) e.printStackTrace(); finally if (reader != n
14、ull) reader.close();if (conn != null) conn.disconnect();return rs;/将 map 型转为请求参数型public static String urlencode(Map data) StringBuilder sb = new StringBuilder();for (Map.Entry i : data.entrySet() try sb.append(i.getKey().append(“=“).append(URLEncoder.encode(i.getValue()+“,“UTF-8“).append(“ catch (UnsupportedEncodingException e) e.printStackTrace();return sb.toString();