将servlet中的数据保存到j2me中的recordstore [英] save data from servlet to recordstore in j2me

查看:60
本文介绍了将servlet中的数据保存到j2me中的recordstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个j2me应用程序,该应用程序正在使用servlet与数据库进行通信. 我想将从Servlet接收的数据存储到记录存储中并显示它.如何实现?请提供代码示例. 预先谢谢

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());
              }
        }

推荐答案

将此函数调用放在显示响应数据警报的位置

Put this function call where you show the response data alert

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

函数定义如下:

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;
        }
    }
}

保存数据后,读取记录存储区RMS-NAME并检查添加的索引以获取响应数据.

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

.

注意::假设网络响应数据将附加到记录存储中.如果要将其设置为特定记录,请相应地修改方法writeToRecordStore(...).

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.

这篇关于将servlet中的数据保存到j2me中的recordstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆