Java tutorial
/* *Copyright 2014 Evan Cleary. * *Licensed under the Apache License, Version 2.0 (the "License"); *you may not use this file except in compliance with the License. *You may obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0 * *Unless required by applicable law or agreed to in writing, software *distributed under the License is distributed on an "AS IS" BASIS, *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *See the License for the specific language governing permissions and *limitations under the License. */ package com.division.jsonrpc; import com.division.jsonrpc.api.JsonRequest; import com.division.jsonrpc.api.RPCCallback; import com.division.jsonrpc.api.RPCTransport; import com.division.jsonrpc.api.SuperclassExclusionStrategy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.stream.JsonReader; import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; /** * Creates a client capable of performing RPC transactions * with the specified server. All requests and responses are * handled asynchronously with a callback system. */ public class JsonClient { private Gson gson; private GsonBuilder gsonBuilder; private final String rpcServer; public JsonClient(String rpcServer) { this.rpcServer = rpcServer; gsonBuilder = new GsonBuilder().disableHtmlEscaping() .excludeFieldsWithModifiers(Modifier.VOLATILE, Modifier.TRANSIENT) .setExclusionStrategies(new SuperclassExclusionStrategy()); gson = gsonBuilder.create(); } public void sendRequest(final JsonRequest request, final Type returnType, final RPCCallback callback) { Thread t = new Thread() { public void run() { RPCTransport response; String jRequest = gson.toJson(request); HttpURLConnection urlConnection = null; try { URL server = new URL(rpcServer); urlConnection = (HttpURLConnection) server.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("content-type", "application/json"); urlConnection.setDoOutput(true); OutputStream oStream = urlConnection.getOutputStream(); System.out.println(jRequest); oStream.write(jRequest.getBytes()); oStream.flush(); oStream.close(); InputStream rStream = urlConnection.getInputStream(); String responseString = ""; int bytes; byte[] bytesRec = new byte[51200]; while ((bytes = rStream.read(bytesRec, 0, bytesRec.length)) != -1) { responseString += new String(bytesRec, 0, bytes); } rStream.close(); responseString = responseString.trim(); if (callback != null) { JsonReader jReader = new JsonReader(new StringReader(responseString)); jReader.setLenient(true); response = gson.fromJson(jReader, returnType); callback.onResponseReceived(response); } urlConnection.disconnect(); } catch (Exception e) { e.printStackTrace(); System.out.println("JSONRPC: EXCEPTION"); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } } }; t.setDaemon(true); t.start(); } }