Troubleshooting guide

124
BlackBerry Java Development Environment Development Guide
{
int status = httpConn.getResponseCode();
switch (status)
{
case (HttpConnection.HTTP_OK):
//Connection is 200 OK.
//Download and process data.
keepGoing = false;
break;
case (HttpConnection.HTTP_UNAUTHORIZED):
//Connection is 401 UnAuthorized.
//A login and password is required.
//Retrieve the login information from somewhere.
//You could prompt the user for this information or
//retrieve this from elsewhere if it is saved within
//your application.
//Login information is hard coded here for brevity, but
//we ask the user if they want to log-in.
UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run()
{
dialogResponse = Dialog.ask
(Dialog.D_YES_NO,"Unauthorized Access:\n Do you wish to log in?");
}
});
if (dialogResponse == Dialog.YES)
{
String login = "username:password";
//Close the connection.
s.close();
//Encode the login information in Base64 format.
byte[] encoded = Base64OutputStream.encode(login.getBytes(), 0,
login.length(), false, false);
//Open a new connection.
s = (StreamConnection)Connector.open("http://mysite.com/myProtectedFile.txt
");
httpConn = (HttpConnection)s;
//Add the authorized header.
httpConn.setRequestProperty("Authorization", "Basic " + new String(encoded));
}
else
{
//Handle failed connection.