📄 receive.java
字号:
/* * ReceiveThread.java * * Created on 2003年11月20日, 下午9:50 */package handenglish;import javax.microedition.lcdui.*;import javax.microedition.io.*;import java.io.*;import javax.microedition.rms.*;import java.util.*;/** * * @author Administrator */public class Receive{ /** Creates a new instance of ReceiveThread */ private Midlet midlet; public final static int CONNECT_ERROR = 1001; public final static int RETURN_ERROR = 1010; public final static short MSG_LOGIN = 2000; public final static short MSG_LIST = 2010; public final static short MSG_WORDS = 2020; public final static short MSG_QUERY = 2030; //private final static String m_ServerUrl="http://211.136.85.101:80/netjdc/servlet/JdcServer"; private final static String m_ServerUrl="http://61.153.21.82:8888/netjdc/servlet/JdcServer"; private ReceiveListener recvListener; private HttpConnection hConn; public Receive(Midlet midlet) { this.midlet = midlet; } private HttpConnection getConnect(String nowServletUrl) { HttpConnection conn = null; try { conn = (HttpConnection)Connector.open(nowServletUrl,Connector.READ_WRITE,true); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"); conn.setRequestProperty("Content-Language", "en-US" ); //conn.setRequestProperty("X-Online-Host","www.easymove.cn:80" ); conn.setRequestProperty("Content-Type", "application/octet-stream" ); conn.setRequestProperty("Connection","Keep-Alive"); } catch(Exception e){ conn = null; } return conn; } public void setReceiveListener( ReceiveListener recvListener ) { this.recvListener = recvListener; } public void checkUpdate() { hConn = getConnect(m_ServerUrl); if (hConn==null) { recvListener.errorReceived( CONNECT_ERROR ); return; } DataOutputStream tempOut = null; DataInputStream tempIn = null; try { tempOut = hConn.openDataOutputStream(); tempOut.writeShort( MSG_LOGIN ); tempOut.writeLong(SysParam.getInstance().lastUpdate); tempOut.writeLong(SysParam.getInstance().msgPostDate); //tempOut.close(); tempOut.flush(); int responseCode = hConn.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { tempIn = hConn.openDataInputStream(); int status = 0; if ( tempIn.readLong()>0){ status = 1; } String tempMsg = tempIn.readUTF(); if ( tempMsg.length() > 0 ) { SysParam.getInstance().isRead = false; SysParam.getInstance().msg = tempMsg; SysParam.getInstance().msgPostDate = System.currentTimeMillis(); SysParam.getInstance().setParameter(); } recvListener.dataReceived( MSG_LOGIN,status ); tempIn.close(); } else { recvListener.errorReceived( responseCode ); } } catch(Exception e){} finally { closeStream( tempIn,tempOut); closeConn(); } } public void getList(int no) { recvListener.dataReceiving( MSG_LIST,0,100,0); hConn = getConnect(m_ServerUrl); if (hConn==null) { recvListener.errorReceived( CONNECT_ERROR ); return; } DataOutputStream tempOut = null; DataInputStream tempIn = null; try { tempOut = hConn.openDataOutputStream(); tempOut.writeShort( MSG_LIST ); tempOut.writeInt( no ); tempOut.flush(); int responseCode = hConn.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { tempIn = hConn.openDataInputStream(); int size = tempIn.readInt(); try { RecordStore.deleteRecordStore( "listdb" ); } catch( Exception e ) {} DbApi listDb = new DbApi( "listdb" ); //listDb.deleteAll(); int[] number=new int[size]; String[] name=new String[size]; int[] volume=new int[size]; for( int i = 0;i< size;i++ ) { number[i]=tempIn.readInt(); name[i]=tempIn.readUTF(); volume[i]=tempIn.readInt(); recvListener.dataReceiving( MSG_LIST,i+1,size,1); } ByteArrayOutputStream tempBos=new ByteArrayOutputStream(); DataOutputStream tempDos=new DataOutputStream(tempBos); for( int i = 0;i< size;i++ ) { tempBos.reset(); tempDos.writeInt(number[i]); tempDos.writeUTF(name[i]); tempDos.writeInt(volume[i]); listDb.addDatabase( tempBos.toByteArray() ); recvListener.dataReceiving( MSG_LIST,i+1,size,2); } listDb.closeDatabase(); tempIn.close(); SysParam.getInstance().lastUpdate = System.currentTimeMillis(); //sysParam.lastUpdate=0; SysParam.getInstance().setParameter(); recvListener.dataReceived( MSG_LIST,0 ); } else recvListener.errorReceived( responseCode ); } catch(Exception e) {} finally { closeStream( tempIn,tempOut); closeConn(); } } public void getWords( String name,int no,int volume ) { recvListener.dataReceiving( MSG_WORDS,0,100,0); hConn = getConnect(m_ServerUrl); if (hConn==null) { recvListener.errorReceived( CONNECT_ERROR ); return; } DataOutputStream tempOut = null; DataInputStream tempIn = null; try { tempOut = hConn.openDataOutputStream(); tempOut.writeShort( MSG_WORDS ); tempOut.writeInt( no ); tempOut.writeInt( volume ); tempOut.flush(); int responseCode = hConn.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { tempIn = hConn.openDataInputStream(); int size = tempIn.readInt(); String[][] a=new String[size][3]; for( int i = 0;i< size ;i++ ) { a[i][0]=tempIn.readUTF(); a[i][1]=tempIn.readUTF(); a[i][2]=tempIn.readUTF(); recvListener.dataReceiving( MSG_WORDS,i+1,size,1); } recvListener.dataReceiving( MSG_WORDS,0,size,2); try{ RecordStore.deleteRecordStore( "wordsdb" ); } catch( Exception e ){} DbApi wordsDb = new DbApi( "wordsdb" ); //wordsDb.deleteAll(); ByteArrayOutputStream tempBos=new ByteArrayOutputStream(); DataOutputStream tempDos=new DataOutputStream(tempBos); for( int i = 0;i< size;i++){ tempBos.reset(); tempDos.writeUTF(a[i][0]); tempDos.writeUTF(a[i][1]); tempDos.writeUTF(a[i][2]); wordsDb.addDatabase( tempBos.toByteArray()); recvListener.dataReceiving( MSG_WORDS,i+1,size,2); } wordsDb.closeDatabase(); tempIn.close(); SysParam.getInstance().storeName = name; SysParam.getInstance().storeNo = no; SysParam.getInstance().storeVolume =volume; SysParam.getInstance().setParameter(); recvListener.dataReceived( MSG_WORDS,0 ); } else recvListener.errorReceived( responseCode ); } catch(Exception e) { Alert alert = new Alert( "错误信息","网络通讯出现错误,请稍后重试",null,AlertType.ERROR ); alert.setTimeout( Alert.FOREVER ); Display.getDisplay( midlet ).setCurrent( alert ); } finally { closeStream( tempIn,tempOut); closeConn(); } } public void queryWord( String word,Vector words) { hConn = getConnect(m_ServerUrl); if (hConn==null) { recvListener.errorReceived( CONNECT_ERROR ); return; } DataOutputStream tempOut = null; DataInputStream tempIn = null; try { tempOut = hConn.openDataOutputStream(); tempOut.writeShort( MSG_QUERY ); tempOut.writeInt( 0 ); tempOut.writeUTF( word ); tempOut.close(); int responseCode = hConn.getResponseCode(); words.removeAllElements(); if (responseCode == HttpConnection.HTTP_OK) { tempIn = hConn.openDataInputStream(); int size = tempIn.readInt(); for( int i = 0;i< size;i++ ) words.addElement( tempIn.readUTF() ); recvListener.dataReceived( MSG_QUERY,0 ); } else recvListener.errorReceived( responseCode ); } catch(Exception e) {} finally { closeStream( tempIn,tempOut); closeConn(); } } public void closeConn() { if ( hConn != null ) { try { hConn.close(); } catch( Exception e ) {} finally { hConn = null; } } } private void closeStream(DataInputStream in,DataOutputStream out) { /* try { if ( in != null ) { in.close(); in = null; } }catch( Exception e ){} */ try { if ( out != null ) out.close(); }catch( Exception e ){} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -