Monday, March 25, 2013

Call HTTP Post method on Blackberry

As Continues work on Blackberry today i need to integrated http POST method on blackberry. If you want to implement POST method on Blackberry following code will very help full to you. Nothing you have to do anything just parse parameter one is Post URL. you can use URL as following format.

http://yourdomainname/webservicenamehere.php

public StringBuffer httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception {
  
  StringBuffer bodyData = new StringBuffer(256);
  ConnectionFactory conFactory = new ConnectionFactory();
  conFactory.setTimeLimit(1000);
  HttpConnection conn = (HttpConnection) conFactory.getConnection(urlStr)
    .getConnection();
  conn.setRequestMethod(HttpConnection.POST);
  conn.setRequestProperty("Content-Type",
    "application/x-www-form-urlencoded");

  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < paramName.length; i++) {
   sb.append(paramName[i]);
   sb.append("=");
   sb.append(paramVal[i]);
   sb.append("&");
  }
  byte[] postData = sb.toString().getBytes("UTF-8");
  conn.setRequestProperty("Content-Length",
    new Integer(postData.length).toString());

  OutputStream out = conn.openOutputStream();
  out.write(postData);
  out.close();

  // This writes to our connection and waits for a response
  if (conn.getResponseCode() != 200) {
   throw new Exception(conn.getResponseMessage());
  }
  InputStream inputStream = conn.openInputStream();
  byte[] data = new byte[256];
  int len = 0;
  int size = 0;
  while (-1 != (len = inputStream.read(data))) {
   bodyData.append(new String(data, 0, len, "UTF-8"));
   size += len;
  }

  if (inputStream != null) {
   inputStream.close();
  }
  return bodyData;
 }
Hope it will help full. don't forget to write comment.

Read All Blackberry Contact using PIM


It easy to read contact on blackberry mobile plz use this following code that read most of parameter that we need to developed mobile app.

System.out.println("********************************************");
  try{
  String numberWork="",numberHome="",numberMobile="",numberOther="",numberPager="",numberFax="";
  BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
  Enumeration enumContacts = contactList.items();
  while (enumContacts.hasMoreElements()) {
   BlackBerryContact c = (BlackBerryContact) enumContacts.nextElement();
   if ((contactList.isSupportedField(BlackBerryContact.NAME))
     && (c.countValues(BlackBerryContact.NAME) > 0)) {
    name = c.getStringArray(BlackBerryContact.NAME, 0);
    String firstName = name[BlackBerryContact.NAME_GIVEN];
    String lastName = name[BlackBerryContact.NAME_FAMILY];
    String fullname = "";
    if (firstName != null) {
     fullname += firstName + " ";
    }

    if ((contactList.isSupportedField(BlackBerryContact.TEL))
      && (c.countValues(BlackBerryContact.TEL) > 0)) {
     int numValues = 0;
     try {
      numValues = c.countValues(BlackBerryContact.TEL);
     } catch (Exception localException) {
     }
     
     for (int i = 0; i < numValues; ++i) {
      if (c.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_WORK)
        numberWork = c.getString(BlackBerryContact.TEL, i);
      else if (c.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_HOME)
        numberHome = c.getString(BlackBerryContact.TEL, i);
      else if (c.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_MOBILE)
       numberMobile = c
         .getString(BlackBerryContact.TEL, i);
      else if (c.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_OTHER)
       numberOther = c.getString(115, i);
      else if (c.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_PAGER)
       numberPager = c.getString(BlackBerryContact.TEL, i);
      else if (c.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_FAX) {
       numberFax = c.getString(BlackBerryContact.TEL, i);
      }
     }

     System.out.println("Mobile Number: "+ numberMobile);
     System.out.println("Work Number: " + numberWork);
     System.out.println("Home Number: " + numberHome);
     System.out.println("Numebr Pager " + numberPager);
     System.out.println("Fax Number" + numberFax);
     System.out.println("Other Number" + numberOther);
    }

   }

  }
  }catch(Exception e){
   System.out.println(e);
  }
  System.out.println("********************************************");
Hope it save your development time.

Parsing JSON object in Blackberry

Some time on sending http request server return JSON object so on blackberry side we need to write code that we can parse that object in display value on screen.

Following code is use full for parsing Json object in blackberry
To implement this code you need following lib on your project so plz download it from github.

https://github.com/upictec/org.json.me/

void parseJSONResponceInBB(String jsonStrFormat){  
     String[] readObject = {"Action"}; // Here my Json have "Action" Object to read you can change according your value.
        try {  
            JSONObject json = new JSONObject(jsonInStrFormat);  
            JSONObject jsonObj  = (JSONObject) json.get("result"); // You can change value according your return object.
            add(new LabelField(jsonObj.get(readObject[1])));
           
        } catch (JSONException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }

Hope this code help full.
There are some application we need to set GMT instated to sent current time. Following code will help to get accurate Blackberry GMT hours.

Calendar rightnow = Calendar.getInstance(TimeZone.getDefault());
int offsetDST = TimeZone.getDefault().getOffset(1, rightnow.get(Calendar.YEAR), rightnow.get(Calendar.MONTH), rightnow.get(Calendar.DAY_OF_MONTH), rightnow.get(Calendar.DAY_OF_WEEK), rightnow.get(Calendar.MILLISECOND));
System.out.println("Offset (Daylight Savings): "+ offsetDST);
System.out.println(("Offset (Without Daylight Savings): "+ TimeZone.getDefault().getRawOffset()));
System.out.println("GNT Time: "+(float)offsetDST/3600000);
  
this.timezone = ""+(float)offsetDST/3600000;

System.out.println("GMT: "+timezone);

Hope this code will help full.