Titanium's native HTTP android module runs on an Apache HTTP library from 2007. The problem with Ti.Network.HTTPClient is it's lack of support for some HTTP methods, mainly no PATCH support.
titanium-okhttp introduces an interchangeable HTTP client that interfaces okhttp. As of Android 4.4, okhttp has been the supporting library. Let's get titanium up to speed!
Note to gittio users: this module is still in development
gittio install com.atticoos.tiokhttp
Download the latest zip in android/dist and unzip in your project root such that it fills/creates a modules directory with its contents
You can seamlessly swap out your existing networking client for titanium-okhttp, and pick up where you left off. No refactoring.
var client,
options = {
onload: function () {
// handle your response
},
onerror: function () {
// handle your error
}
};
var client
if (Ti.Platform.osname === 'android') {
client = require('com.atticoos.tiokhttp').createHttpClient(options);
} else {
client = Ti.Network.createHttpClient(options);
}
client.setRequestHeader('Content-Type', 'application/json; charset=utf8');
client.open('GET', url);
client.send(data);Nothing has changed, other than conditionally creating the client with a different library for android.
If you're starting a new project and don't care to have backward compatability, you'll find a much nicer request API:
An example GET request:
var client = TitaniumOkHttp.createOkHttpClient();
client.GET(url, {
onSuccess: function (data, status, headers) {
// your response data
}
});An example POST request, with headers:
var client = TitaniumOkHttp.createOkHttpClient({
defaultHeaders: {
'Content-Type': 'application/json'
}
});
client.POST(url, {
data: JSON.stringify({foo: 'bar'}),
headers: {
'Authorization': 'Access-Token xxx'
},
onSuccess: function (data, status, headers) {
// your response data
},
onError: function (error, status, headers) {
// your error response data
}
});