将servlet中的数据保存到j2me中的记录存储中

发布于 2024-12-20 16:18:46 字数 1003 浏览 3 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-12-27 16:18:46

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

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并检查添加的索引以获取响应数据。

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

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.

趴在窗边数星星i 2024-12-27 16:18:46
   //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);
 }
   //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);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文