Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created August 15, 2014 01:55
Show Gist options
  • Select an option

  • Save s-shin/97d64be4629bc1b8a9e1 to your computer and use it in GitHub Desktop.

Select an option

Save s-shin/97d64be4629bc1b8a9e1 to your computer and use it in GitHub Desktop.
package com.example.jsonrpc;
import android.content.Context;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class JSONRPC {
public static final String TAG = JSONRPC.class.getName();
static final String END_POINT = "http://jsonrpc.example.com";
//----------------------------------------
// Singleton
private static JSONRPC mInstance = new JSONRPC();
private JSONRPC() {}
public static JSONRPC getInstance() { return mInstance; }
//----------------------------------------
// Interfaces
public static class Error extends Exception {
public Error(String message) {
super(message);
}
}
public interface Handler {
JSONObject buildParams() throws JSONException;
void success(JSONObject result);
void failure(Error error);
}
//----------------------------------------
// Public Methods
public static void call(String method, final Handler handler) {
JSONObject body = new JSONObject();
try {
body.put("id", "Generated ID");
body.put("jsonrpc", "2.0");
body.put("method", method);
body.put("params", handler.buildParams());
} catch (JSONException e) {
handler.failure(new Error(e.toString()));
return;
}
Log.i(TAG, "Request to '" + END_POINT + "': " + body.toString());
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, END_POINT, body, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Response: " + response.toString());
try {
if (response.has("error")) {
handler.failure(new Error(response.getJSONObject("error").toString()));
} else {
handler.success(response.getJSONObject("result"));
}
} catch (JSONException e) {
handler.failure(new Error(e.toString()));
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
handler.failure(new Error(error.networkResponse.toString()));
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
if (!getInstance().addRequest(req)) {
handler.failure(new Error("Request queue has not been created."));
}
}
//----------------------------------------
// Private Methods
private RequestQueue mQueue;
private boolean addRequest(Request req) {
if (mQueue == null) {
Context context = App.getInstance().getContext(); // Application Context
if (context == null) {
return false;
}
mQueue = Volley.newRequestQueue(context);
}
mQueue.add(req);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment