47804aaebbd1b98d0434d5c096762d9cefefd554.svn-base 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.yihu.utils;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import com.yihu.base.ConfigUtil;
  6. import com.yihu.wsgw.api.ServiceBus;
  7. import net.sf.json.JSONObject;
  8. public class ApiUtil {
  9. /**
  10. * 返回结果
  11. *
  12. * @param code
  13. * @param msg
  14. * @return
  15. */
  16. public static JSONObject jsonResult(int code, String msg) {
  17. JSONObject obj = new JSONObject();
  18. obj.put("Code", code);
  19. obj.put("Message", msg);
  20. return obj;
  21. }
  22. /**
  23. * 调用接口
  24. *
  25. * @param appId 渠道号
  26. * @param apiName api名
  27. * @param params 参数
  28. * @return
  29. * @throws Exception
  30. */
  31. public static String callApi(String apiName,String params) throws Exception {
  32. return callApi(null,apiName,params);
  33. }
  34. /**
  35. * 调用接口
  36. *
  37. * @param appId 渠道号
  38. * @param apiName api名
  39. * @param params 参数
  40. * @return
  41. * @throws Exception
  42. */
  43. public static String callApi(String appId,String apiName,String params) throws Exception {
  44. if (appId == null) {
  45. appId = ConfigUtil.getInstance().getAppId();
  46. }
  47. String result = null;
  48. result = ServiceBus.getInstance(
  49. ConfigUtil.getInstance().getCenterServerUrl().toString(),
  50. appId).call(apiName, params, true);
  51. return result;
  52. }
  53. public static Map<String ,String> changeToMap(String params){
  54. Map<String, String> paramMap=new HashMap<String, String>();
  55. JSONObject json = JSONObject.fromObject(params);
  56. Iterator<String> keys=json.keys();
  57. while(keys.hasNext()){
  58. String key=keys.next();
  59. Object val= json.get(key);
  60. paramMap.put(key, val==null?null:val.toString());
  61. }
  62. return paramMap;
  63. }
  64. }