Skip to content

Instantly share code, notes, and snippets.

@bdbergeron
Created August 14, 2014 21:02
Show Gist options
  • Save bdbergeron/057770461fcdae787958 to your computer and use it in GitHub Desktop.
Save bdbergeron/057770461fcdae787958 to your computer and use it in GitHub Desktop.
JSON-RPC and Volley
import com.android.volley.Response;
import com.android.volley.toolbox.JsonRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.UUID;
public abstract class JsonRpcRequest<T> extends JsonRequest<T> {
private static final String JSONRPC_PARAM_ID = "id";
private static final String JSONRPC_PARAM_METHOD = "method";
private static final String JSONRPC_PARAM_PARAMETERS = "params";
public JsonRpcRequest (final String url, final String method, final JSONObject jsonRequest,
final Response.Listener<T> listener,
final Response.ErrorListener errorListener) throws JSONException {
super(Method.POST, url, new JSONObject() {
{
putOpt(JSONRPC_PARAM_ID, UUID.randomUUID().toString());
putOpt(JSONRPC_PARAM_METHOD, method);
putOpt(JSONRPC_PARAM_PARAMETERS, jsonRequest);
}
}.toString(), listener, errorListener);
}
}
import org.json.JSONException;
import org.json.JSONObject;
public class JsonRpcResponse {
private static final String JSONRPC_PARAM_ID = "id";
private static final String JSONRPC_PARAM_ERROR = "error";
private static final String JSONRPC_PARAM_RESULT = "result";
private String mId;
private JSONObject mError;
private JSONObject mResult;
public JsonRpcResponse (final String jsonString) throws JSONException {
final JSONObject jsonObject = new JSONObject(jsonString);
mId = jsonObject.getString(JSONRPC_PARAM_ID);
mError = jsonObject.optJSONObject(JSONRPC_PARAM_ERROR);
if (mError != null) {
return;
}
mResult = jsonObject.getJSONObject(JSONRPC_PARAM_RESULT);
}
public String getId () {
return mId;
}
public JSONObject getError () {
return mError;
}
public JSONObject getResult () {
return mResult;
}
}
@lazarodev
Copy link

How can i implement this in an activity? thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment