Java me 在j2me中将数据从servlet保存到recordstore
我正在开发一个j2me应用程序,它使用servlet与数据库通信。 我想将从servlet接收到的数据存储到记录存储并显示它。如何实现这一点?请提供代码示例。 先谢谢你Java me 在j2me中将数据从servlet保存到recordstore,java-me,Java Me,我正在开发一个j2me应用程序,它使用servlet与数据库通信。 我想将从servlet接收到的数据存储到记录存储并显示它。如何实现这一点?请提供代码示例。 先谢谢你 public void viewcon() { StringBuffer sb = new StringBuffer(); try { HttpConnection c = (HttpConnection) Connector.open(url); c.setRequestProperty("User-Ag
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());
}
}
将此函数调用放在显示响应数据警报的位置
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(…)
。//此代码在显示类中引发异常。
//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);
}
尝试
{
字节[]byteInputData=新字节[5];
整数长度=0;
//访问记录存储中存在的所有记录
for(int x=1;x byteInputData.length)
{
byteInputData=新字节[rs.getRecordSize(x)];
}
长度=rs.getRecord(x,byteInputData,0);
}
alert=新警报(“正在读取”,新字符串(byteInputData,0,长度),null,AlertType.WARNING);
alert.setTimeout(alert.FOREVER);
显示。设置当前(警报);
}
感谢您的帮助..我做了更改n现在记录已存储,但在读取记录时会抛出TRACE:,在显示类java.lang.illegalargumentException中捕获异常很好,您能够解决它。对于其他问题,请检查您是否引用了正确的记录索引,同时检查从记录中读取的字节数组是否已转换为适当的数据类型。@DhanaashreePanPatil,如果您已经解决了问题,请标记它。