Created
August 14, 2014 21:02
-
-
Save bdbergeron/057770461fcdae787958 to your computer and use it in GitHub Desktop.
JSON-RPC and Volley
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can i implement this in an activity? thank you :)