-3

I am developing an j2me application which is communicating with the database using servlet. I want to store the data received from servlet into record store and display it.how this can be achieved?Please provide code examples. Thank you in advance

 public void viewcon()
 {
  StringBuffer sb = new StringBuffer();

  try {
  HttpConnection c = (HttpConnection) Connector.open(url);
  c.setRequestProperty("User-Agent","Profile/MIDP-1.0, Configuration/CLDC-1.0");
  c.setRequestProperty("Content-Language","en-US");
  c.setRequestMethod(HttpConnection.POST);
  DataOutputStream os = (DataOutputStream)c.openDataOutputStream();

  os.flush();
  os.close();

  // Get the response from the servlet page.
  DataInputStream is =(DataInputStream)c.openDataInputStream();
  //is = c.openInputStream();
   int ch;
   sb = new StringBuffer();
   while ((ch = is.read()) != -1) {
   sb.append((char)ch);
       }
  // return sb;
   showAlert(sb.toString());//display data received from servlet 

    is.close();
    c.close();

              } catch (Exception e) {
                  showAlert(e.getMessage());
              }
        }
  • @DhanaashreePanPatil, if you have solved the problem, [accept the correct answer](http://stackoverflow.com/faq#howtoask) and mark it. – Vimal Dec 11 '11 at 15:10

2 Answers2

0

Put this function call where you show the response data alert

writeToRecordStore(sb.toString().getBytes());

The function definition is as below:

private static String RMS_NAME = "NETWORK-DATA-STORAGE";
private boolean writeToRecordStore(byte[] inStream) {
    RecordStore rs = null;
    try {
        rs = RecordStore.openRecordStore(RMS_NAME, true);
        if (null != rs) {
            //Based on your logic either ADD or SET the record
            rs.addRecord(inStream, 0, inStream.length);
            return true;
        } else {
            return false;
        }
    } catch (RecordStoreException ex) {
        ex.printStackTrace();
        return false;
    } finally {
        try {
            if (null != rs) {
                rs.closeRecordStore();
            }
        } catch (RecordStoreException recordStoreException) {
        } finally {
            rs = null;
        }
    }
}

After you have saved the data, read the records store RMS-NAME and check the added index to get the response data.

.

NOTE: The assumption is the network response data is to be appended to the record store. If you want to set it to a particular record modify the method writeToRecordStore(...) accordingly.

Vimal
  • 1,266
  • 1
  • 9
  • 16
  • Thanks for your help..i did the changes n now the records are getting stored but on reading them it throws TRACE: , Exception caught in Display class java.lang.IllegalArgumentException – Dhanaashree Mahirrao Dec 11 '11 at 11:43
  • Good that you were able to solve it. For your other problem check if you are referring the correct record index, also check if the byte array read from the records are converted to appropriate data type. – Vimal Dec 11 '11 at 15:04
0
   //this code throws exception in display class. 
    try
    {
     byte[] byteInputData = new byte[5];
    int length = 0;
// access all records present in record store
    for (int x = 1; x <= rs.getNumRecords(); x++)
    {
      if (rs.getRecordSize(x) > byteInputData.length)
      {
        byteInputData = new byte[rs.getRecordSize(x)];
      }
      length = rs.getRecord(x, byteInputData, 0);
    }

     alert =new Alert("reading",new String(byteInputData,0,length),null,AlertType.WARNING);
     alert.setTimeout(Alert.FOREVER);
     display.setCurrent(alert);
 }