Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/telamon/android-ajax.request
REST helper for Android, inspired by prototypejs' Ajax.Request
https://github.com/telamon/android-ajax.request
Last synced: about 1 month ago
JSON representation
REST helper for Android, inspired by prototypejs' Ajax.Request
- Host: GitHub
- URL: https://github.com/telamon/android-ajax.request
- Owner: telamon
- License: mit
- Created: 2011-05-10T01:56:08.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2011-07-25T21:14:55.000Z (over 13 years ago)
- Last Synced: 2024-04-29T22:21:51.876Z (8 months ago)
- Language: Java
- Homepage:
- Size: 154 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Ajax.Request for Android
========================
A sweet way of writing your everyday http requests.I'm used to writing webservice requests in JavaScript and when I arrived
to the android platform I realized that the HttpClient-request-way left much
to be desired compared to prototypejs' Ajax.Request with auto-serialization, content handling and
general lazyness.Features
---------
* Automatic content handling for XML and JSON
* Built on top of AsyncTask to ensure that your callback code runs on the UI thread.
* Provides general lazyness.
* Failsafe? (No guarantees yet)
* Better resource handling to be able to receive binary data responses?
### TODO* JSON Header support
* More ways to handle XML post data?
* Rails authenticity token handling for "application/x-www-form-urlencoded" postdataRequesting Things (The Request class)
-------------------------------------### Example usage
Include the library in whatever fashion you desire#### Simple GET request
Request r = new Request("http://mydomain.org/service/service.json"){
// Optional callback override.
@Override
protected void onSuccess(Transport transport) {
// Your handling code goes here,
// The 'transport' object holds all the desired response data.
EditText et = (EditText) this.findViewById(R.id.editText1);
et.getText().append( transport.getResponseJson().optString("foo") );
}
}.execute("GET");
#### Simple POST requestContext context = getApplicationContext();
startMyProgressIndicator();
Request r = new Request("http://mydomain.org/service/highscore"){
// Optional callback override.
@Override
protected void onSuccess(Transport transport) {
// Grab the desired nodes with the xpath helper
org.w3c.dom.NodeList users = transport.findByXpath('//users');
// do something.
for(org.w3c.dom.Node user:users){
addUserToMyList(user);
}
}
// Optional callback override.
@Override
protected void onComplete(Transport transport) {
stopMyProgressIndicator();
}
// Optional callback override.
@Override
protected void onError(IOException ex) {
Toast.makeText(context, "Error occured: " + ex.getMessage(), Toast.LENGTH_SHORT);
}
// Optional callback override.
@Override
protected void onFailure(Transport transport) {
Toast.makeText(context, "Something went wrong. code: " + transport.getStatus(),Toast.LENGTH_SHORT);
}
}
// Tell the webserver that we would like to have the response in XML
r.accept(Request.CTYPE_XML);
// Tell the webserver that our post data is in JSON
r.setContentType(Request.CTYPE_JSON);
r.execute("POST","{ \"name\" : \"MrMan\", \"score\" : 9001 }");
### Headers
You can set whatever HTTPHeader you like but they must be set _before_ the execute() call.request.setHeader("If-Modified-Since", "Tue, 10 May 2011 11:31:56 GMT")
There are a few shortcuts like
request.accept("text/plain"); // Modifies the 'Accept' header
setContentType("application/json"); // Modifies the 'Content-Type' header### Parameters
You can easily set POST or PUT data by calling
r.setParams(myBadAssObject)
or provide your data as the second parameter to execute, eg:
r.execute("POST",myKewlObject)
setParams(Object) method tries to automatically determine the type of content you're
trying to send, and automatically sets the Content-Type header accordingly.The detected data is then delegated to one of the following methods:
#### Urlencoded form
setFormParams(new String[][]{
{"foo","bar"},
{"eat","bacon"}
});
Converts your data to an UrlEncodedFormEntity and calls setContent(CTYPE_FORM).
setFormParams(HashMap, ?>)
Does the same as above except it takes an hashmap as input and ultimately calls
toString() on each key / value.#### JSON
setJsonParams(org.json.JSONObject)
Sets Content-Type header to application/json.
Converts your JSONObject to a String and then delegates your data to setStringParams()#### XML
setXmlParams(org.w3c.dom.Document)
Sets Content-Type header to application/xml,
Converts your Xml Document to a String and then delegates your data to setStringParams()#### Plain text
setStringParams(String)
Creates a StringEntity from your data and sets it to be sent with the request.
#### note
As of this moment there's no sophisticated string content type detection.
The only case that setStringParams() modifies the "Content-Type" header
is when the string begins with __'