Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **
- * Simple JSON RPC Client
- */
- public class JSONRPCClient {
- private String mBaseUrl;
- private String mMethod;
- private HashMap<String, Integer> mIntParams;
- private HashMap<String, String> mStrParams;
- public JSONRPCClient() {
- mBaseUrl = Cons.API_URL;
- mIntParams = new HashMap<String, Integer>();
- mStrParams = new HashMap<String, String>();
- }
- public void setBaseUrl(String url) {
- mBaseUrl = url;
- }
- public void setMethod(String method) {
- mMethod = method;
- }
- public void addParam(String name, String value) {
- mStrParams.put(name, value);
- }
- public void addParam(String name, int value) {
- mIntParams.put(name, value);
- }
- public JSONObject connect() throws Exception {
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(mBaseUrl);
- JSONObject jsonResult = null;
- try {
- JSONObject params = buildParam();
- String entity = params.toString();
- Debug.i("POST " + mBaseUrl);
- Debug.i("Data: " + entity);
- HttpParams httpParams = httpClient.getParams();
- HttpConnectionParams.setConnectionTimeout(httpParams, Cons.HTTP_CONNECTION_TIMEOUT);
- HttpConnectionParams.setSoTimeout(httpParams, Cons.HTTP_SOCKET_TIMEOUT);
- httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
- httpPost.setEntity(new StringEntity(entity));
- HttpResponse httpResponse = httpClient.execute(httpPost);
- HttpEntity httpEntity = httpResponse.getEntity();
- if (httpEntity == null) throw new Exception("");
- InputStream stream = httpEntity.getContent();
- if (stream != null) {
- String strResponse = StringUtil.streamToString(stream);
- Debug.i(strResponse);
- JSONObject jsonResponse = (JSONObject) new JSONTokener(strResponse).nextValue();
- if (!jsonResponse.isNull("result")) {
- jsonResult = jsonResponse.getJSONObject("result");
- }
- stream.close();
- }
- } catch (Exception e) {
- throw e;
- }
- return jsonResult;
- }
- private JSONObject buildParam() {
- JSONObject object = new JSONObject();
- JSONObject params = new JSONObject();
- try {
- if (mIntParams.size() > 0) {
- for (String key : mIntParams.keySet()) {
- params.put(key, mIntParams.get(key));
- }
- }
- if (mStrParams.size() > 0) {
- for (String key : mStrParams.keySet()) {
- params.put(key, mStrParams.get(key));
- }
- }
- object.put("jsonrpc", "2.0");
- object.put("method", mMethod);
- object.put("params", params);
- object.put("id", 7);
- } catch (Exception e) {
- }
- return object;
- }
- }
- //-------------- CLIENT ------------------
- JSONRPCClient jsonRpc = new JSONRPCClient();
- jsonRpc.setMethod("printSlipCheck"); //set method name
- //set parameters
- jsonRpc.addParam("check", check);
- jsonRpc.addParam("id", 7);
- JSONObject jsonObj = jsonRpc.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement