Advertisement
eshuliakovsky

JSON-RPC

Apr 9th, 2020
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. **
  2.  * Simple JSON RPC Client
  3.  */
  4. public class JSONRPCClient {
  5.     private String mBaseUrl;
  6.     private String mMethod;
  7.  
  8.     private HashMap<String, Integer> mIntParams;
  9.     private HashMap<String, String> mStrParams;
  10.  
  11.     public JSONRPCClient() {
  12.         mBaseUrl = Cons.API_URL;
  13.  
  14.         mIntParams = new HashMap<String, Integer>();
  15.         mStrParams = new HashMap<String, String>();
  16.     }
  17.  
  18.     public void setBaseUrl(String url) {
  19.         mBaseUrl = url;
  20.     }
  21.  
  22.     public void setMethod(String method) {
  23.         mMethod = method;
  24.     }
  25.  
  26.     public void addParam(String name, String value) {
  27.         mStrParams.put(name, value);
  28.     }
  29.  
  30.     public void addParam(String name, int value) {
  31.         mIntParams.put(name, value);
  32.     }
  33.  
  34.     public JSONObject connect() throws Exception {
  35.         HttpClient httpClient = new DefaultHttpClient();
  36.         HttpPost httpPost = new HttpPost(mBaseUrl);
  37.         JSONObject jsonResult = null;
  38.  
  39.         try {
  40.             JSONObject params = buildParam();
  41.             String entity = params.toString();
  42.  
  43.             Debug.i("POST " + mBaseUrl);
  44.             Debug.i("Data: " + entity);
  45.  
  46.             HttpParams httpParams = httpClient.getParams();
  47.  
  48.             HttpConnectionParams.setConnectionTimeout(httpParams, Cons.HTTP_CONNECTION_TIMEOUT);
  49.             HttpConnectionParams.setSoTimeout(httpParams, Cons.HTTP_SOCKET_TIMEOUT);
  50.  
  51.             httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
  52.             httpPost.setEntity(new StringEntity(entity));
  53.  
  54.             HttpResponse httpResponse = httpClient.execute(httpPost);
  55.  
  56.             HttpEntity httpEntity = httpResponse.getEntity();
  57.  
  58.             if (httpEntity == null) throw new Exception("");
  59.  
  60.             InputStream stream = httpEntity.getContent();
  61.  
  62.             if (stream != null) {
  63.                 String strResponse = StringUtil.streamToString(stream);
  64.  
  65.                 Debug.i(strResponse);
  66.  
  67.                 JSONObject jsonResponse = (JSONObject) new JSONTokener(strResponse).nextValue();
  68.  
  69.                 if (!jsonResponse.isNull("result")) {
  70.                     jsonResult = jsonResponse.getJSONObject("result");
  71.                 }
  72.  
  73.                 stream.close();
  74.             }
  75.         } catch (Exception e) {
  76.             throw e;
  77.         }
  78.  
  79.         return jsonResult;
  80.     }
  81.  
  82.     private JSONObject buildParam() {
  83.         JSONObject object = new JSONObject();
  84.         JSONObject params = new JSONObject();
  85.  
  86.         try {
  87.             if (mIntParams.size() > 0) {
  88.                 for (String key : mIntParams.keySet()) {
  89.                     params.put(key, mIntParams.get(key));
  90.                 }
  91.             }
  92.  
  93.             if (mStrParams.size() > 0) {
  94.                 for (String key : mStrParams.keySet()) {
  95.                     params.put(key, mStrParams.get(key));
  96.                 }
  97.             }
  98.  
  99.             object.put("jsonrpc", "2.0");
  100.             object.put("method", mMethod);
  101.             object.put("params", params);
  102.             object.put("id", 7);
  103.  
  104.         } catch (Exception e) {
  105.         }
  106.  
  107.         return object;
  108.     }
  109. }
  110.  
  111. //-------------- CLIENT ------------------
  112.  
  113.                 JSONRPCClient jsonRpc = new JSONRPCClient();
  114.  
  115.                 jsonRpc.setMethod("printSlipCheck"); //set method name
  116.  
  117.                 //set parameters
  118.                 jsonRpc.addParam("check",  check);
  119.                 jsonRpc.addParam("id",  7);
  120.                 JSONObject jsonObj = jsonRpc.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement