第一,我是 Java 新手。我学到的一切都归功于 Netbeans 和互联网。 我在用 NetBeans IDE 7.0.1 jdk1.7.0_25 Java_ME_platform_SDK_3.2
项目 1st-InsrtDB
package insrtdb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class InsrtDB {
private static Statement stmt = null;
private static Connection con = null;
public static String mt="0:00:00";
public static String sms="Test InsrtDB";
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException, Exception {
// TODO code application logic here
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:MADb");
System.out.println("Connected!");
stmt = con.createStatement();
int rowsEffected = 0;
// Example INSERT new record
rowsEffected = updateData("INSERT INTO Reqst (myTime,SMS) VALUES (' "+mt+" ',' "+sms+" ')");
System.out.print("Inserted a Row : ");
con.close();
}
public static int updateData(String SQL) throws Exception {
return stmt.executeUpdate(SQL);
}
}
输出-InsrtDB(运行) 运行: 连接的! 插入一行:构建成功(总时间:27 秒)
项目 2nd-SndRecvSMS(MIDlet 应用程序)
private class SmsReceiverThread extends Thread {
public void run() {
try {
// Receive Message
TextMessage txtMsg = (TextMessage) receiveCon.receive();
// Get the receiving SMS phone number
String senderTpNo = txtMsg.getAddress();
// Get the receiving SMS message
String senderMsg = txtMsg.getPayloadText();
// Create a TextBox and direct the incomming message to that
switchDisplayable(null, getTxtBox());
smt = senderMsg.substring(10);
msgSnt = senderMsg.substring(0, 10);
stringItem.setText(smt);
stringItem1.setText(msgSnt);
}
catch (IOException ex) {
}
}
}
我第一次在 JavaMEPhone1 上运行这个 midlet,而不是在 JavaMEPhone2 上运行,我可以成功发送和接收 SMS。 现在,当在第二个应用程序中时,我在资源中添加第一个应用程序作为添加项目和清理和构建我得到 输出:- 构建成功(总时间:8 秒)
现在,当我添加以粗体突出显示的代码时
private class SmsReceiverThread extends Thread {
public void run() {
try {
// Receive Message
TextMessage txtMsg = (TextMessage) receiveCon.receive();
// Get the receiving SMS phone number
String senderTpNo = txtMsg.getAddress();
// Get the receiving SMS message
String senderMsg = txtMsg.getPayloadText();
// Create a TextBox and direct the incomming message to that
switchDisplayable(null, getTxtBox());
smt = senderMsg.substring(10);
msgSnt = senderMsg.substring(0, 10);
stringItem.setText(smt);
stringItem1.setText(msgSnt);
} catch (IOException ex) {
}
insrtdb.InsrtDB.mt=smt;
System.out.println("New value of mt = " +insrtdb.InsrtDB.mt);
insrtdb.InsrtDB.sms=msgSnt;
System.out.println("New value 0f sms = " +insrtdb.InsrtDB.sms);
try {
insrtdb.InsrtDB.class.getClass().newInstance();
} catch (IllegalAccessException ex) {
} catch (InstantiationException ex) {
}
}
}
清理、构建并运行。
输出:- 对于 JavaMEPhone1
运行
123456790
012345678917:12:50
运行:
构建成功(总时间:8分14秒)
输出:- 对于 JavaMEPhone2
收到消息
mt 的新值 = 17:12:50
新值 0f 短信 = 0123456789
运行-不构建:
构建成功(总时间:2分34秒)
但已连接!
插入的行缺少行且数据库中未插入行。
我缺少什么?
最佳答案
我一直在寻找基本、简单且可行的解决方案。但首先让我给你两个引用。 第 1:-如何在 J2ME 中发送和接收短信,作者:anujarosha,网址: http://anujarosha.wordpress.com/2011/07/20/how-to-send-and-receive-sms-in-j2me/ 这是一个彻底而详细的博客,同样值得赞赏。 2nd:-从移动设备发送短信到计算机-java移动套接字级编程,作者:Sanjeewa Malalgoda http://sanjeewamalalgoda.blogspot.in/2009/09/send-text-message-from-mobile-device-to.html 这是一个非常简单的博客,其不言自明,再次受到同样的赞赏。 这两个博客激励我与互联网上的每个人分享我的解决方案。 实际上这是我正在尝试构建的演示的一部分,它有 4 个步骤。 首先:移动 midlet 应用程序将 SMS 发送到第二个移动设备。 第二:第二个移动 midlet 应用程序接收此 SMS(如果应用程序正在运行)。此第二个移动设备通过 USB 数据线连接到笔记本电脑。 第三:收到短信后,应用程序向服务器套接字(在笔记本电脑上运行的java应用程序)发送一个套接字。 第四:收据上的服务器套接字拆分并插入到数据库表中,该表有 2 个字段,一个是 myTime,另一个是 SMS。 现在我要处理三个时代。第一个移动设备时间(发送者时间)、第二个移动设备时间(接收移动设备时间)和第三个笔记本电脑时间,当然这三个时间都不同步。对我来说,发送者时间是一个重要因素,因此我必须将其纳入短信本身。同样,SMS 字符串长度可以在 0 到 160 之间变化;因此,SMS 本身也包含了同样的内容。因此字符串短信有3部分:字符串“短信长度”+字符串“sms”+字符串“时间”(hh:mm:ss)。
我的解决方案是第一个java应用程序,如下
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package aserver;
import java.net.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author A S Patel
*/
public class AServer {
private static ServerSocket serverSocketin;
private static Statement stmt = null;
private static Connection con = null;
public static String mt;
public static String sms;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
serverSocketin = new ServerSocket(8206);
DataRiciver dataRiciver=new DataRiciver();
dataRiciver.start();
}catch (IOException e) {
}
}
static class DataRiciver extends Thread {
@Override
public void run(){
while (true){
try {
Socket incoming = serverSocketin.accept();
DataInputStream in = new DataInputStream(incoming.getInputStream());
String data="";
data = in.readLine();
System.out.println(data);
String lN=data.substring(0, 3);
int n=Integer.parseInt(lN);
mt=data.substring(n+3);
sms=data.substring(3, n+3);
InsertDB();
in.close();
incoming.close();
Thread.sleep(1000);
}catch (Exception e) {
}
}
}
}
public static void InsertDB() throws ClassNotFoundException, SQLException, Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:MADb");
System.out.println("Connected!");
stmt = con.createStatement();
int rowsEffected = 0;
// Example INSERT new record
rowsEffected = updateData("INSERT INTO Reqst (myTime,SMS) VALUES (' "+mt+" ',' "+sms+" ')");
System.out.print("Inserted a Row : ");
con.close();
}
public static int updateData(String SQL) throws Exception {
return stmt.executeUpdate(SQL);
}
}
现在第二个是 Midlet 代码。就像在 Neatbeans 中一样,如果您使用可视化 Midlet,流程图将会是!
现在在新消息表单中选择发送并右键单击并选择转到源
添加这段代码
} else if (command == send) {
// write pre-action user code here
Calendar cal = Calendar.getInstance();
int hr = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
int snd = cal.get(Calendar.SECOND);
String mt = String.valueOf(hr)+":"+String.valueOf(min)+":"+String.valueOf(snd);
phnNoStr = phnNoTexField.getString();
msgStr = msgTexField.getString() ;
int n=msgStr.length();
String lN;
if(n<10){lN="00"+ String.valueOf(n);}else
if(n<100){lN="0"+ String.valueOf(n);}else
{lN= String.valueOf(n);}
mySMS=lN+msgStr+mt;
// write post-action user code here
new SMSThread(phnNoStr, mySMS).start();
switchDisplayable(null, getSuccess());
}
在代码的末尾部分,即 public void destroyApp(boolean unconditional) 之后
你有常规内部类私有(private)类SmsReceiverThread扩展Thread
在后面添加以下代码
String senderMsg = txtMsg.getPayloadText();
// Create a TextBox and direct the incomming message to that
switchDisplayable(null, getTxtBox());
String lN=senderMsg.substring(0, 3);
int n=Integer.parseInt(lN);
smt = senderMsg.substring(n+3);
msgSnt = senderMsg.substring(3, n+3);
stringItem.setText(smt);
stringItem1.setText(msgSnt);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
}
String data=senderMsg;
sendData(data);
} catch (IOException ex) {
}
switchDisplayable(null, getSplashScreen());
}
}
public void sendData(String data) {
try {
StreamConnection connection = (StreamConnection) Connector.open("socket://localhost:8206");
PrintStream output = new PrintStream(connection.openOutputStream());
data=data +"\n";
output.write(data.getBytes());
output.flush();
output.close();
connection.close();
} catch (Exception e) {
}
您可以在 Neatbeans 中运行第一个代码,而不是 2 个 Midlet 实例
关于java - 如何从 Midlet 运行 java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21457378/