国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

用J2ME的通用聯網框架開發聯網的應用程序

2019-11-18 19:35:51
字體:
來源:轉載
供稿:網友

  盡管目前的無線網絡不夠理想,手機聯網還是給我們開發人員不小的震撼的。畢竟這真的是件神奇的事情,不是嗎?本文將講述如何應用J2ME平臺中的通用聯網框架開發聯網的應用程序。

  首先,必須說明一點:MIDP中規定,任何移動信息設備都必須提供通過http協議的支持,而像其他的通信方式例如socket是設備相關的。有些手機會支持,有些則不支持。這里只大概的說明一下http協議相關的內容,如果不了解這個方面的知識請參考http協議。在javax.microedition.io里面是大量的接口,只有一個connector類,當然在midp2.0里面添加了對push技術的支持,這個留做以后講。connector類提供的最重要的方法是open()方法,它的返回值為Connection,你可以對他進行轉換得到你需要的類型,比如我們以http協議訪問服務器。

void postViaHttpConnection(String url) throws IOException {
 HttpConnection c = null;
 InputStream is = null;
 OutputStream os = null;
 int rc;

 try {
  c = (HttpConnection)Connector.open(url);

  // Set the request method and headers
  c.setRequestMethod(HttpConnection.POST);
  c.setRequest  c.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
  c.setRequestProperty("Content-Language", "en-US");

  // Getting the output stream may flush the headers
  os = c.openOutputStream();
  os.write("LIST games/n".getBytes());
  os.flush(); // Optional, getResponseCode will flush

  // Getting the response code will open the connection,
  // send the request, and read the HTTP response headers.
  // The headers are stored until requested.
  rc = c.getResponseCode();
  if (rc != HttpConnection.HTTP_OK) {
   throw new IOException("HTTP response code: " + rc);
  }

  is = c.openInputStream();

  // Get the ContentType
  String type = c.getType();
  processType(type);

  // Get the length and process the data
  int len = (int)c.getLength();
  if (len > 0) {
   int actual = 0;
   int bytesread = 0 ;
   byte[] data = new byte[len];
   while ((bytesread != len) && (actual != -1)) {
    actual = is.read(data, bytesread, len - bytesread);
    bytesread += actual;
   }
   process(data);
   } else {
    int ch;
    while ((ch = is.read()) != -1) {
     process((byte)ch);
    }
   }
  } catch (ClassCastException e) {
   throw new IllegalArgumentException("Not an HTTP URL");
  } finally {
   if (is != null)
    is.close();
   if (os != null)
    os.close();
   if (c != null)
    c.close();
  }
}

  上面的代碼是我取自API doc(建議多讀一下api doc)。
  下面根據自己的經驗說明一下聯網中比較重要的問題:

  我們應該明白這是如何工作的,手機發送請求通過無線網絡傳輸到運營商的WAP網關,WAP網關將請求轉發到web服務器,服務器可以用cgi,asp,servlet/jsp等構建。服務器處理后會把響應轉發到WAP網關,WAP網關再把它發送到手機上。WAP網關對我們開發人員來說是透明的我們不用管它。

  如果在你的聯網程序上看不到Thread,Runnable這樣的字眼,那么你的程序是不能運行的。因為考慮到網絡的因素,為了避免操作堵塞。你必須把聯網動作放到另外一個線程去運行,而不能在主線程運行。最好當聯網的時候提供給用戶一個等待的界面比如作一個動畫界面。我下面提供的例子中沒有用,因為我想把這個單獨出來以后談。

  通常聯網的應用程序的界面是比較多的,最好我們使用MVC的模式來實現界面的導航。

  考慮好你想如何傳遞你數據,這一點是非常重要的。你可以用GET方法也可以使用POST方法,推薦后者。因為get方法只能是通過URL編碼的傳輸。而POST更加靈活,配合DataInputStream、DataOutputStream來使用更是方便。必須清楚我們如何接受數據是跟數據如何發送過來相關的,例如在client端writeUTF(message);writeInt(4);writeBoolean(true),那么接受就應該readUTF();readInt();readBoolean();如果發送過來數據長度是可用的,那么我們可以建立一個適當的數組來接受,如果不可用我們就要一個適當容量的數組來接受。

  下面我提供一個實例來說明以上的問題,在應用程序中我們輸入任意字符,通過連接server得到響應并顯示出來。server我用servlet寫的非常簡單,只是在受到的內容后面加上“haha”,程序我安裝到自己的手機上運行沒有任何問題,就是GPRS網絡不夠快。Server是tomcat5實現的,關于如何部署servlet的問題超出了本文的討論范圍。我只提供代碼,推薦用Eclipse+Lomboz開發j2ee程序。

/*
* Created on 2004-7-5
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
package com.north;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class MyServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request,
 HttpServletResponse response) throws ServletException, IOException {
  DataInputStream dis = new DataInputStream(request.getInputStream());
  String result = dis.readUTF();
  DataOutputStream dos = new DataOutputStream(response.getOutputStream());
  dos.writeUTF(result+"haha");
  dos.close();
  dis.close();
  //TODO Method stub generated by Lomboz
 }
 protected void doPost(HttpServletRequest request,
  HttpServletResponse response) throws ServletException, IOException {
   doGet(request,response);
   //TODO Method stub generated by Lomboz
  }
}

  聯網的時候一定按照如下的流程做

  1、建立連接,設置傳輸方式推薦POST,設置方法頭

  2、打開輸出流,傳輸數據給服務器

  3、判斷相應的狀態碼,進入不同流程控制,注意錯誤處理。如果OK則開始接受數據

  4、關閉連接和流

  下面是客戶端的代碼,對錯誤處理沒有考慮太多。一共是5個class

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/*
* Created on 2004-7-4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class HttpCommMIDlet extends MIDlet
{

private UIController uicontroller;
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException
{
 // TODO Auto-generated method stub
 uicontroller = new UIController(this);
 uicontroller.init();
}

/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp()
{
 // TODO Auto-generated method stub
}

/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
 // TODO Auto-generated method stub
}

}

import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;

/*
* Created on 2004-7-4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class UIController
{
 private HttpCommMIDlet midlet;
 private InputCanvas inputUI;
 private DisplayCanvas displayUI;
 private Display display;
 private HttpCommHandler httpHandler;

/**
*
*/
public UIController(HttpCommMIDlet midlet)
{
 this.midlet = midlet;
 // TODO Auto-generated constructor stub
}

public static class EventID
{
 public static final int CONNECT_TO_SERVER = 0;
 public static final int DISPLAY_BACK_TO_INPUT = 1;
}

public void init()
{
 display = Display.getDisplay(midlet);
 httpHandler = new HttpCommHandler(
  " inputUI = new InputCanvas(this);
 displayUI = new DisplayCanvas(this);
 display.setCurrent(inputUI);
}

public void setCurrent(Displayable disp)
{
 display.setCurrent(disp);
}

public void handleEvent(int EventID, Object[] obj)
{
 new EventHandler(EventID, obj).start();
}

private class EventHandler extends Thread
{
 private int eventID;
 private Object[] obj;
 private Displayable backUI;

 public EventHandler(int eventID, Object[] obj)
 {
  this.eventID = eventID;
  this.obj = obj;
 }

public void run()
{
 synchronized (this)
 {
  run(eventID, obj);
 }
}

private void run(int eventID, Object[] obj)
{
 switch (eventID)
 {
  case EventID.CONNECT_TO_SERVER:
  {
   try
   {
    String result = httpHandler.sendMessage((String) obj[0]);
    displayUI.init(result);
    setCurrent(displayUI);
    break;
   } catch (IOException e)
   {
   }
  }
  case EventID.DISPLAY_BACK_TO_INPUT:
  {
   setCurrent(inputUI);
   break;
  }
  default:
   break;
  }
 }
};

}

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/*
* Created on 2004-7-4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class InputCanvas extends Form implements CommandListener
{
 private UIController uicontroller;
 private TextField inputField;
 private StringItem result;
 public static final Command okCommand = new Command("OK", Command.OK, 1);

 public InputCanvas(UIController uicontroller)
 {
  super("Http Comunication");
  this.uicontroller = uicontroller;
  inputField = new TextField("Input:", null, 20, TextField.ANY);
  this.append(inputField);
  this.addCommand(okCommand);
  this.setCommandListener(this);
 }

/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1)
{
 // TODO Auto-generated method stub
 if (arg0 == okCommand)
 {
  String input = inputField.getString();
  uicontroller.handleEvent(UIController.EventID.CONNECT_TO_SERVER,
  new Object[] { input });
 }
}

}

import java.io.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/*
* Created on 2004-7-4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class HttpCommHandler
{
 private String URL;
 public HttpCommHandler(String URL)
 {
  this.URL = URL;
 }

 public String sendMessage(String message) throws IOException
 {
  HttpConnection httpConn;
  DataInputStream input;
  DataOutputStream output;
  String result;
  try
  {
   httpConn = open();
   output = this.openDataOutputStream(httpConn);
   output.writeUTF(message);
   output.close();
   input = this.openDataInputStream(httpConn);
   result = input.readUTF();
   closeConnection(httpConn,input,output);
   return result;
  }
 finally
 {
  }
}

public HttpConnection open() throws IOException
{
 try
 {
  HttpConnection connection = (HttpConnection) Connector.open(URL);
  connection.setRequestProperty("User-Agent", System
.getProperty("microedition.profiles"));
  connection.setRequestProperty("Content-Type",
"
application/octet-stream");
  connection.setRequestMethod(HttpConnection.POST);
  return connection;
 } catch (IOException ioe)
 {
  throw ioe;
 }
}

private DataInputStream openDataInputStream(HttpConnection conn)
throws IOException
{
 int code = conn.getResponseCode();
 if (code == HttpConnection.HTTP_OK)
 {
  return conn.openDataInputStream();
 } else
 {
  throw new IOException();
 }
}

private DataOutputStream openDataOutputStream(HttpConnection conn)
throws IOException
{
 return conn.openDataOutputStream();
}

private void closeConnection(HttpConnection conn, DataInputStream dis,
DataOutputStream dos)
{
 if(conn!= null)
 {
  try
  {
   conn.close();
  }
  catch(IOException e)
  {}
 }
 if(dis!=null)
 {
  try
  {
   dis.close();
  }
  catch(IOException e)
  {}
 }
 if(dos!=null)
 {
  try
  {
   dos.close();
  }
  catch(IOException e)
  {}
 }
}

}

 import javax.microedition.lcdui.Command;
 import javax.microedition.lcdui.CommandListener;
 import javax.microedition.lcdui.Displayable;
 import javax.microedition.lcdui.Form;
 import javax.microedition.lcdui.StringItem;

 /*
  * Created on 2004-7-6
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - Java - Code Style - Code Templates
 */

 /**
  * @author P2800
  *
  * TODO To change the template for this generated type comment go to Window -
  * Preferences - Java - Code Style - Code Templates
 */
 public class DisplayCanvas extends Form implements CommandListener
 {
  private UIController uicontroller;
  private StringItem result;
  private int index = 0;
  private boolean first = true;
  public static Command backCommand = new Command("Back", Command.BACK, 2);

  public DisplayCanvas(UIController uicontroller)
  {
   super("Result");
   this.uicontroller = uicontroller;
   result = new StringItem("you have input:", null);
   this.addCommand(backCommand);
   this.setCommandListener(this);
  }

 public void init(String message)
 {
  if (first)
  {
   result.setText(message);
   index = this.append(result);
   first = false;
  }
  else
  {
   this.delete(index);
   result.setText(message);
   this.append(result);
  }
 }

 /*
  * (non-Javadoc)
  *
  * @see   javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
  * javax.microedition.lcdui.Displayable)
 */
 public void commandAction(Command arg0, Displayable arg1)
 {
  // TODO Auto-generated method stub
  uicontroller.handleEvent(UIController.EventID.DISPLAY_BACK_TO_INPUT,null);
 }
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰城市| 太保市| 封丘县| 苍梧县| 中山市| 杭州市| 临江市| 通海县| 广宁县| 临城县| 临泉县| 宁远县| 兴山县| 临颍县| 鹤峰县| 高邮市| 五寨县| 吴忠市| 琼中| 岑巩县| 大渡口区| 美姑县| 牡丹江市| 蒙自县| 怀柔区| 监利县| 武安市| 清苑县| 开化县| 开江县| 花莲市| 尼玛县| 广宗县| 阿坝| 东兰县| 双柏县| 高碑店市| 乌拉特中旗| 苍山县| 二手房| 芜湖县|