Skip to content

Instantly share code, notes, and snippets.

@soulfly
Created November 26, 2012 13:12
Show Gist options
  • Save soulfly/4148120 to your computer and use it in GitHub Desktop.
Save soulfly/4148120 to your computer and use it in GitHub Desktop.
BlackBerry 5,6,7 Chat sample: Create session, User Sign Up, User Sign In
// Make request
public void doRequest(int type)
{
switch(type)
{
// Create session request
case REQUEST_SESSION:
{
String time = new Long(new Date().getTime()).toString();
String timestamp = time.substring(0, time.length()-3);
String postParam = "application_id=" + APP_ID + "&auth_key=" + AUTH_KEY + "&nonce=" + nonce + "&timestamp=" + timestamp;
String signature = "";
try
{
signature = hmacsha1(AUTH_SECRET, postParam);
}
catch(Exception ex)
{
System.out.println("---------> SIGNATURE ERROR");
break;
}
JSONObject postObject = new JSONObject();
try
{
postObject.put("application_id", APP_ID);
postObject.put("auth_key", AUTH_KEY);
postObject.put("nonce", nonce);
postObject.put("timestamp", timestamp);
postObject.put("signature", signature);
}
catch(Exception ex)
{
}
BigVector postData = new BigVector();
postData.addElement(postObject.toString());
DebugStorage.getInstance().Log(0, "Post params: " + postParam);
DebugStorage.getInstance().Log(0, "Post data: " + postObject.toString());
HTTPConnManager man = new HTTPConnManager(POST, "http://api.quickblox.com/session.json", postData, type, this);
break;
}
// User Sign Up
case REQUEST_USERS:
{
JSONObject credentialsObject = new JSONObject();
JSONObject postObject = new JSONObject();
try
{
credentialsObject.put("login", usernameField.getText());
credentialsObject.put("password", passwordField.getText());
postObject.put("user", credentialsObject);
}
catch(Exception ex)
{
}
BigVector postData = new BigVector();
postData.addElement(TOKEN);
postData.addElement(postObject.toString());
DebugStorage.getInstance().Log(0, "Post data: " + postObject.toString());
HTTPConnManager man = new HTTPConnManager(POST, "http://api.quickblox.com/users.json", postData , type, this);
break;
}
// User Sign In
case REQUEST_LOGIN:
{
String time = new Long(new Date().getTime()).toString();
String timestamp = time.substring(0, time.length()-3);
String postParam = "application_id=" + APP_ID + "&auth_key=" + AUTH_KEY + "&nonce=" + nonce + "&timestamp=" + timestamp;
String signature = "";
try
{
signature = hmacsha1(AUTH_SECRET, postParam);
}
catch(Exception ex)
{
System.out.println("---------> SIGNATURE ERROR");
break;
}
JSONObject postObject = new JSONObject();
try
{
postObject.put("application_id", APP_ID);
postObject.put("auth_key", AUTH_KEY);
postObject.put("nonce", nonce);
postObject.put("timestamp", timestamp);
postObject.put("signature", signature);
postObject.put("login", usernameField.getText());
postObject.put("password", passwordField.getText());
}
catch(Exception ex)
{
}
BigVector postData = new BigVector();
postData.addElement(TOKEN);
postData.addElement(postObject.toString());
DebugStorage.getInstance().Log(0, "Post params: " + postParam);
DebugStorage.getInstance().Log(0, "Post data: " + postObject.toString());
HTTPConnManager man = new HTTPConnManager(POST, "http://api.quickblox.com/login.json", postData, type, this);
break;
}
default: break;
}
}
public void ProcessAnswer(int type, String result)
{
switch(type)
{
// Create Session answer OK
case REQUEST_SESSION:
{
// Do something
break;
}
// User Sign Up answer OK
case REQUEST_USERS:
{
// Do something
break;
}
// User Sign In answer OK
case REQUEST_LOGIN:
{
// Do something
break;
}
default: break;
}
}
public void ProcessError(int type, final String message)
{
// Process errors
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment