edu.asu.bscs.ihattend.jsonrpcapp.JsonRPCCalculator.java Source code

Java tutorial

Introduction

Here is the source code for edu.asu.bscs.ihattend.jsonrpcapp.JsonRPCCalculator.java

Source

/*
 *      The MIT License (MIT)
 *
 *
 *      Copyright (c) 2015 Ian Hattendorf
 *
 *       Permission is hereby granted, free of charge, to any person obtaining a copy
 *      of this software and associated documentation files (the "Software"), to deal
 *      in the Software without restriction, including without limitation the rights
 *      to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *      copies of the Software, and to permit persons to whom the Software is
 *      furnished to do so, subject to the following conditions:
 *
 *      The above copyright notice and this permission notice shall be included in
 *      all copies or substantial portions of the Software.
 *
 *      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *      THE SOFTWARE.
 *
 *      @author Ian Hattendorf mailto:Ian.Hattendorf@asu.edu
 *
 *       @version February 9, 2015
 */

package edu.asu.bscs.ihattend.jsonrpcapp;

import android.util.Log;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;

public class JsonRPCCalculator {

    private final JsonRpcRequestViaHttp server;

    private static final String TAG = JsonRPCCalculator.class.getName();

    private URL url;

    private int id = 0;

    public JsonRPCCalculator(String serviceUrl) {
        try {
            url = new URL(serviceUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        server = new JsonRpcRequestViaHttp(url);
    }

    public Double calculate(Double op1, Double op2, String operation) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("jsonrpc", "2.0");
            jsonObject.put("id", ++id);
            jsonObject.put("method", operation);

            String params = String.format(",\"params\":[%.2f,%.2f]", op1, op2);
            String almost = jsonObject.toString();
            String begin = almost.substring(0, almost.length() - 1);
            String end = almost.substring(almost.length() - 1);
            String call = begin + params + end;

            Log.d(TAG, "call: " + call);
            String responseString = server.call(call);
            Log.d(TAG, "response: " + responseString);
            JSONObject response = new JSONObject(responseString);
            Double result = response.optDouble("result");
            Log.d(TAG, "result: " + result);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        // should really throw an exception...
        return 0.0;
    }
}