繁体   English   中英

如何从Midlet运行Java应用程序

[英]How to run java Application from Midlet

首先,我是Java的新手。 我学到的一切都归功于Netbeans和Internet。 我正在使用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);
     }
}

OUTPUT-InsrtDB(运行)运行:已连接! 插入一行:BUILD SUCCESSFUL(总时间: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和JavaMEPhone2上运行了这个Midlet 1st,并且我可以成功发送和接收SMS。 现在,当在第二个应用程序中时,我在“资源”中添加第一个应用程序,作为添加项目和“清理与构建”,我得到输出:-BUILD SUCCESSFUL(总时间: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) {
       }

    }
}

清理并生成并运行。 输出:-对于运行123456790 012345678917:12:50的JavaMEPhone1运行:构建成功(总时间:8分14秒)输出:-对于JavaMEPhone2收到的消息mt的新值= 17:12:50零值短信= 0123456789运行-不构建:成功构建(总时间:2分34秒)
但是连接! 已插入行,行缺失,未插入数据库行。

我缺少什么?

好吧,我一直在寻找基本,简单且可行的解决方案。 但是首先,我要给您2个参考。 1st:-如何在http://anujarosha.wordpress.com/2011/07/20/how-to-send-and-receive-sms-in-j2me/中通过anujarosha在J2ME中发送和接收SMS详细的博客并赞赏相同。 第二个:-将文本消息从移动设备发送到计算机-Java移动套接字级别由Sanjeewa Malalgoda进行编程,网址为http://sanjeewamalalgoda.blogspot.in/2009/09/send-text-message-from-mobile-device-to.html这是一个非常简单的Blog,它具有自我解释功能,再次赞赏。 这两个博客启发了我与互联网上的每个人分享我的解决方案。 实际上,这是我要构建的演示的一部分,它有4个步骤。 第一:移动Midlet应用程序将SMS发送到2nd Mobile。 第二:第二台移动Midlet应用程序接收此SMS(如果应用程序正在运行)。该第二台移动设备通过USB数据线连接到笔记本电脑。 第三:收到短信后,应用程序将套接字发送到服务器套接字,这是在笔记本电脑上运行的Java应用程序。 第四:收据上的服务器套接字拆分并插入数据库表,该表具有2个字段,一个是myTime,第二个是SMS。 现在我必须处理三遍。 第一个手机时间(发送者时间),第二个手机时间(接收手机时间)和第三个笔记本电脑时间,自然所有3次都不同步。 对我而言,发件人时间是重要的因素,因此我必须将其纳入SMS本身。 同样,SMS字符串的长度可以从0到160不等; 因此,SMS本身也包含了相同的内容。 因此,字符串SMS有3个部分:字符串“ SMS长度” +字符串“ 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());
            }                                           

在代码的最后部分,即在公共void destroyApp(boolean unconditional)之后

您有常规的内部类私有类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实例中的第一个代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM