📄 getinfodemo.java
字号:
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import javax.microedition.rms.*;
public class GetInfoDemo extends MIDlet implements CommandListener{
private Display display;
//天气Url
public String strWeatherUrl =
"http://3g.sina.com.cn/prog/wapsite/weather_new/forecast.php?city=%B1%B1%BE%A9&vt=1";
//主显示窗口
private TextBox tbMain;
//接收短消息手机号码
private TextField tfMobileNumber;
//接收短消息内容
TextField tfMsgContent;
//退出按钮
private Command cmdExit = new Command("退出",Command.EXIT,1);
//发送消息按钮
private Command cmdSendMsg = new Command("发送短信",Command.SCREEN,1);
//获取信息按钮
private Command cmdGetInfo = new Command("获取天气",Command.SCREEN,1);
//窗体f
private Form f = new Form("获取天气小应用程序");
//记录rs
RecordStore rs;
//
InputStream inputstream = null;
//HTTP返回信息
public String strWeatherHTTP ="";
//手机是否支持文件系统
public boolean isSupp;
//文件系统类fc
FileConnection fc = null;
//手机数据文件
public String strFileName = "file:///data.txt";
/**用记录管理系统RMS来储存数据*/
//构造函数
public GetInfoDemo() {
// TODO Auto-generated constructor stub
super();
boolean isRMS = isRMSValue();
if (isRMS==false){
//RMS变量不存在则写
writeRMS(strWeatherUrl);
} else {
//String strrms = readRMS();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
//自启动函数
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
tbMain = new TextBox("返回信息结果","",255,TextField.ANY);
//程序一启动就获取天气信息
//GetHTTPInfo(strWeatherUrl);
//tbMain.setString(strWeatherHTTP);
//添加按钮
tbMain.addCommand(cmdExit);
tbMain.addCommand(cmdSendMsg);
tbMain.addCommand(cmdGetInfo);
tbMain.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(tbMain);
}
//获取信息
public void GetHTTPInfo(String strUrl)
{
//在手机CMWAP上网测试返回java.lang.StringIndexOutOfBoundsException
//CMNET正常
String content = "";
String strResultHTTP = "";
StringBuffer strbufContent = new StringBuffer();
StringBuffer strbufResultHTTP = new StringBuffer();
StringBuffer strbufSubcontent1 = new StringBuffer();
StringBuffer strbufSubcontent2 = new StringBuffer();
StringBuffer strbufSubcontent3 = new StringBuffer();
try {
String cnwapUrl ="http://3g.sina.com.cn/"+"prog/wapsite/weather_new/forecast.php?city=%B1%B1%BE%A9&vt=1";
HttpConnection hc = (HttpConnection)Connector.open(cnwapUrl);
//////支持CMWAP
hc.setRequestProperty("X-OnLine-Host", "3g.sina.com.cn");
hc.setRequestProperty("Accept", "*/*");
hc.setRequestMethod(HttpConnection.GET);
//////////////
if(hc.getResponseCode()==HttpConnection.HTTP_OK){
DataInputStream dis = new DataInputStream(hc.openInputStream());
byte[] myData = new byte[2000];
dis.read(myData,0,myData.length);
content = new String(myData,"UTF-8");
strbufContent.append(content);
//System.out.println(content);
String strbr = "<br/>";
//定位第一个br位置
int indexbr1 = strbufContent.toString().indexOf(strbr);
//截取第一个br位置后的子字符串
String subcontent1 = strbufContent.toString().substring(indexbr1+5);
strbufSubcontent1.append(subcontent1);
//定位第二个br位置
int indexbr2 = strbufSubcontent1.toString().indexOf(strbr);
String subcontent2 = strbufSubcontent1.toString().substring(indexbr2+5);
strbufSubcontent2.append(subcontent2);
//定位第三个br位置
int indexbr3 = strbufSubcontent2.toString().indexOf(strbr);
String subcontent3 = strbufSubcontent2.toString().substring(indexbr3+5);
strbufSubcontent3.append(subcontent3);
int indexbr4 = strbufSubcontent3.toString().indexOf(strbr);
//获取城市、天气的位置
int indexDateBegin= indexbr1+5+indexbr2+5;
int indexDateEnd = indexbr1+5+indexbr2+5+indexbr3;
int indexWeaBegin = indexbr1+5+indexbr2+5+indexbr3+5;
int indexWeaEnd = indexbr1+5+indexbr2+5+indexbr3+5+indexbr4;
//获取城市、天气
String strDate = strbufContent.toString().substring(indexDateBegin, indexDateEnd);
String strWeather = strbufContent.toString().substring(indexWeaBegin, indexWeaEnd);
//strResultHTTP = strDate +" "+strWeather;
tbMain.setString(strWeather);
} else {
tbMain.setString("不能访问数据");
}
}
catch(Exception e)
{
//System.out.println(e.toString()) ;
tbMain.setString(e.toString());
}
} //获取信息结束
//处理命令按钮事件
public void commandAction(Command c, Displayable s){
if (c== cmdExit){
//退出
notifyDestroyed();
} else if (c==cmdSendMsg){
new Thread(){
public void run(){
//发送短信
boolean isSendSucceed = sendMsg("这是使用J2ME发给您的短信!","10668111");
if (isSendSucceed) {
tbMain.setString("发送成功!");
} else
{
tbMain.setString("发送失败!");
}
}
}.start();
} else if (c==cmdGetInfo){
new Thread(){
public void run(){
//获取天气
GetHTTPInfo(strWeatherUrl);
}
}.start();
}
}
//发送短信,传入短信内容及接收号码
public synchronized boolean sendMsg(String content,String phoneNumber){
//返回值
boolean result =true;
try{
//地址
String address = "sms://" +phoneNumber;
//建立连接
MessageConnection conn = (MessageConnection)Connector.open(address);
//设置短信息类型为文本,短信息有文本和二进制两种类型
TextMessage msg = (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
//设置信息内容
msg.setPayloadText(content);
//发送
conn.send(msg);
conn.close();
}catch(Exception e){
e.printStackTrace();
result = false;
//未处理
}
return result;
}
//检测系统是否有RMS变量GetInfoDemo
private boolean isRMSValue(){
boolean tmpret= false;
try{
rs = RecordStore.openRecordStore("GetInfoDemo",true);
if (rs!=null){
//读取RMS
tmpret = true;
}
rs.closeRecordStore();
} catch(RecordStoreException rse){
System.out.println(rse);
}
return tmpret;
}
//读RMS
private String readRMS(){
String tmpRet="";
try{
rs = RecordStore.openRecordStore("GetInfoDemo",true);
if (rs!=null){
//读取RMS
tmpRet =new String(rs.getRecord(1),"UTF-8");
//tbMain.setString(tmpRet);
}
rs.closeRecordStore();
} catch(Exception rse){
System.out.println(rse);
}
return tmpRet;
}
//写RMS
private void writeRMS(String strData){
try{
//打开记录集GetInfoDemo
rs = RecordStore.openRecordStore("GetInfoDemo", true);
//添加记录集
byte[] data =strData.getBytes();
String s1 = new String(data,"UTF-8");
rs.addRecord(data, 0, data.length);
rs.closeRecordStore();
}catch(Exception e){
System.out.println(e);
}
}
//检测手机是否支持文件系统
public boolean isSupportFileSystem(){
String v = System
.getProperty("microedition.io.file.FileConnection.version");
if(v!=null)return true;
return false;
}
//读文件 ,目前存在不能识别文件路径
private void readFile(){
try{
fc = (FileConnection)Connector.open("file:///c:/data.txt");
System.out.println("读文件"+strFileName);
if (fc.exists()){
System.out.println("文件存在");
java.io.DataInputStream dis = fc.openDataInputStream();
int size = dis.available();
byte[] data = new byte[size];
dis.read(data);
dis.close();
//处理data中数据
System.out.println(data);
}
}catch(IOException ioe){
}
}//读文件结束
//写文件,目前存在不能识别文件路径
private void writeFile(){
try{
fc = (FileConnection)Connector.open("file:///c:/data.txt");
if (fc.exists()){
java.io.InputStream is = fc.openInputStream();
int ch;
//每次读取一个字符
while ((ch=is.read())!=-1){
//处理文件内容
}
is.close();
}
fc.close();
}catch(IOException ioe){
}
}//写文件结束
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -