Skip to content

Instantly share code, notes, and snippets.

@darko1002001
Created September 11, 2011 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darko1002001/1209836 to your computer and use it in GitHub Desktop.
Save darko1002001/1209836 to your computer and use it in GitHub Desktop.
Basic type of custom requests
GCRest client = new GCRest("SOME ENDPOINT FORMED URL");
// url example http://api.edge.getchute.com/v1/chutes/6/contributors
// instance example with url GCRest client = new
// GCRest("http://api.edge.getchute.com/v1/chutes/6/contributors");
try {
// Add optional params
client.addParam("name", "value");
// Add optional headers
client.addHeader("name", "value");
client.execute(RequestMethod.GET);
// Can be executed with GET, POST, PUT, DELETE
// the response data
client.getResponse();
// response code
client.getResponseCode();
// response message
client.getErrorMessage();
} catch (GCApiException e) {
Log.w(TAG, "", e);
}
To generate a custom request extend GCBaseRequest:
class ChutesGetRequest extends GCBaseRequest {
@SuppressWarnings("unused")
private static final String TAG = ChutesGetRequest.class.getSimpleName();
private final String id;
public ChutesGetRequest(GCAccount user, String id) {
super(user, RequestMethod.GET);
this.id = id;
//set the request method
}
@Override
protected void prepareParams() {
add your url params and headers to the client
client.setUrl(String.format(GCRestConstants.URL_CHUTES_GET_SPECIFIC, id));
}
}
to execute:
Create instance
GCBaseRequest request = new ChutesGetRequest(user,id);
request.setParser(new MyCustomParser());
// a custom object implementing GCBaseParser Interface
Data is extracted from:
responseModel.getResponseObject();
needs to be casted into the concrete class;
execute sync:
request.execute();
execute async:
new GCAsyncRunner(request,new GCAsyncCallback(){
@Override
public void onComplete(GCResponse responseModel) {
}
@Override
public void onFail(String message, GCRequestParameters requestParameters,
GCResponse responseModel) {
}
}).execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment