Skip to content

Instantly share code, notes, and snippets.

@grantges
Created September 14, 2011 14:29
Show Gist options
  • Save grantges/1216716 to your computer and use it in GitHub Desktop.
Save grantges/1216716 to your computer and use it in GitHub Desktop.
HTTPClient example with Authorization
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
var win = Ti.UI.createWindow({
title: "MainWindow",
layout: "vertical"
});
var username = Ti.UI.createTextField({
left: 10,
right: 10,
top: 10,
height: 15,
color: "#f4f4f4",
font: {fontSize: 14, fontFamily:"Calibri", fontWeight: "normal"},
value: "USERNAME",
borderColor: "#f4f4f4",
borderWidth: 1,
value: 'myusername'
});
var password = Ti.UI.createTextField({
left: 10,
right: 10,
top: 10,
height: 15,
color: "#f4f4f4",
font: {fontSize: 14, fontFamily:"Calibri", fontWeight: "normal"},
value: "PASSWORD",
borderColor: "#f4f4f4",
borderWidth: 1,
value: 'mypassword'
});
var responseLabel = Ti.UI.createLabel({
color: "#f4f4f4",
font: {fontSize: 14, fontFamily:"Calibri", fontWeight: "normal"},
text: "Response",
left: 10,
top:10,
height: 15,
});
var response = Ti.UI.createTextArea({
title: "ResponseTA",
left: 10,
right: 10,
top: 10,
bottom: 25,
height: 300,
borderColor: "#f4f4f4",
borderWidth: 1,
font: {fontSize: 12, fontFamily:"Calibri", fontWeight: "normal"},
color: "#f4f4f4",
softKeyboardOnFocus: false,
backgroundColor: "#000"
})
var button = Ti.UI.createButton({
title: "Request",
backgroundColor: "#fff",
width: 150,
height: 25,
});
button.addEventListener("click", function(e){
Ti.API.info("Button Clicked");
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function(e) {
Ti.API.info("Status --> "+ this.status );
Ti.API.info("Response:\n " + this.responseText);
response.value = this.responseText;
};
xhr.onerror = function(e) {
Ti.API.error("--> " + this.status + " " + e.message );
response.value = this.status + " " + e.message;
};
xhr.open('GET','http://go.urbanairship.com/api/app/content');
var auth = 'Basic '+ Ti.Utils.base64encode(username.value+":"+password.value);
Ti.API.info("Authorization: "+auth);
xhr.setRequestHeader('Authorization', auth);
xhr.send();
});
win.add(url);
win.add(username);
win.add(password);
win.add(responseLabel);
win.add(response);
win.add(button);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment