Monday, April 11, 2011

Store Data with Persistent API in Blackberry

1. Make Model Class with Persistable interface Implementaion

public class User implements Persistable {
String userName;
String password;
String casinoCode;
boolean saveUserInfo;
public boolean isSaveUserInfo() {
return saveUserInfo;
}
public void setSaveUserInfo(boolean saveUserInfo) {
this.saveUserInfo = saveUserInfo;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCasinoCode() {
return casinoCode;
}
public void setCasinoCode(String casinoCode) {
this.casinoCode = casinoCode;
}
}



2. Store object with Persistent API on Load


PersistentObject _store = PersistentStore.getPersistentObject(PERSISTENT_STORE_ID);
synchronized(_store)
{
try {
if(_store.getContents() == null)
{
User user= new User();
user.setCasinoCode("");
user.setPassword("");
user.setUserName("");
user.setSaveUserInfo(false);
_store.setContents(user);
_store.commit();
}

} catch (Exception e) {
PhotoAPP.errorDialog(e.toString());
}
}


3. Save Data with Peristent API

public static void persist(User user)
{
_store = PersistentStore.getPersistentObject(PERSISTENT_STORE_ID);
synchronized( _store )
{
_store.setContents(user);
PersistentObject.commit(_store);
}
}


4. Retrieve data with Persistent API

public static User getPersistentObject() {
_store = PersistentStore.getPersistentObject(PERSISTENT_STORE_ID);
User user = (User)_store.getContents();
return user;
}

File Upload With Blackberry

String url ="http://www.sitename.com/upload_bb_photos.php";
url= url+"?arg1=arg1";

if (DeviceInfo.isSimulator()) {
url = url + ";deviceSide=true";
}else{
// url = url + ";interface=wifi";
}

HttpConnection http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("User-Agent", "APP_NAME");
FileConnection fconnFrom = null;
int fileSize = 0;
fconnFrom = (FileConnection) Connector.open("pic_full_path");
if (fconnFrom.exists()) {
fileSize = (int) fconnFrom.fileSize();
http.setRequestProperty("Content-Length",
String.valueOf(fileSize));
}
OutputStream oStrm = http.openOutputStream();
byte imgData[] = new byte[fileSize];
DataInputStream dataIn = fconnFrom.openDataInputStream();
dataIn.read(imgData, 0, fileSize);
int index = 0;
int size = 1024;
do {
if ((index + size) > imgData.length) {
size = imgData.length - index;
}
oStrm.write(imgData, index, size);
oStrm.flush();
index += size;
} while (index < imgData.length);

Center Align of Element HorizontalManager

HorizontalFieldManager hf = new HorizontalFieldManager(Field.FIELD_HCENTER);
ButtonField backButtonField= new ButtonField("Back", Field.FIELD_HCENTER );
hf.add(backButtonField);