HTTP network development small example

xiaoxiao2021-04-09  524

It is well known that mobile phones that support MIDP 1.0 If you want to use networking applications, you can only use HTTP services. Whether it's the highest score of uploading games, or dynamic download map resources, image resources need to communicate with HTTP protocols. Refer to the relevant documentation of MIDP1.0 (including Sun and Nokia), let us know that it is generally necessary to select a server such as Tomcat to run Java servlet as a mobile phone and various services including access to the database, download the image service intermediary, in the format We often use the format such as "text / plain", you may also add "GB2312" and the like with Chinese related characters, etc., please refer to J2EE's related knowledge about servers. When using Text / Plain, we actually get a document, we can read anything we need from this document.

When we do network development, we need to define some communication protocols, the simplest example, we may need to download maps and conversation characters and images in the RPG, we send one: "get | strings | ID = 5" (Note Yes The content in our http package), then the server returns a string to complete HTTP communication; "Get | PIC | ID = 100", the server returns a two-way BYTE [] can be. In summary, the communication of the server and mobile phone can be summarized: the operation of binary flow to binary flow two into the flow.

If we communicate with a single thread, a few seconds, the user will inevitably feel very unbearable, we must use multithreaded way to make users can do something else, not simple waiting, even if they join Dynamic reality is also much more much better than simple communications. Multithreading is reflected in the article in which I handle the status of the Loading, you can refer to the idea.

Code: Java Server ....

Import javax.servlet. *; import javax.servlet.http. *; import java.io. *; import java.util. *;

Public class httpgameserver extend.http.httpservlet {public void httpservlet () {}

public void doPost (HttpServletRequest req, HttpServletResponse resp) {int infoLength = req.getContentLength (); System.out.println ( "req.getContentLength ()" infoLength);; ResourceBundle rb = ResourceBundle.getBundle ( "LocalStrings", req .getLocale ()); resp.setContentType ( "text / plain"); try {InputStream is = req.getInputStream (); byte [] bInfoBytes = new byte [infoLength]; // is.read (bInfoBytes); DataInputStream dis = New DataInputStream (IS); System.out.Println ("System Running ..."); // Processing of the input stream //printwriter out = Resp.GetWriter (); Out.write ("test ok"); } Catch (exception ex) {system.out.println (ex.getMessage ());}}} J2ME:

Import javax.microedition.io. *; import java.io. *;

Public class http {httpConnection httpconn; public http (string url) {try {postviahttpConnection (URL);} catch (exception ex) {ex.printstacktrace ();}}

Void PostViahttpConnection (String URL) THROWS IOEXCEPTION {httpConnection C = NULL; InputStream IS = NULL; OUTPUTSTREAM OS = NULL;

Try {c = (httpConnection) Connector.Open (URL); C.SetRequestMethod (httpconnection.post); C.SetRequestProperty ("if-modified-since", "29 OCT 1999 19:43:31 GMT"); c. SetRequestProperty ("User-Agent", "Profile / MIDP-1.0 Configuration / CLDC-1.0"); C.SetRequestProperty ("Content-Language", "EN-US);

// getting the output stream may flush the headers OS = C.OpenOutputStream (); DataoutputStream DOS = New DataOutputStream (OS); Os.Write ("Hello, World" .getbytes ()); // Some mobile phones, add FLUSH may cause HTTP Server to get request information. //OS.Flush ();// nokia needs to join IS = C.OpenInputStream ();

String type = c.gettype (); system.out.println ("TYPE:" TYPE); DataInputStream Dis = New DataInputStream (IS); INT length = disp.available (); Byte [] reponsebytes = new byte [length " ]; Dis.read (reponsebytes); system.out.println ("received.:" New string (reponsebytes));

} finally {if (is! = null) is.close (); if (os! = null) os.close (); if (c! = null) c.close ();}}

}

Import javax.microedition.midlet. *;

public class Main extends MIDlet {public Main () {} protected void pauseApp () {/ ** @ todo Implement this javax.microedition.midlet.MIDlet abstract method * /} protected void startApp () throws javax.microedition.midlet.MIDletStateChangeException {/ ** @ Todo Implement this javax.microedition.midlet.mIdlet Abstract method * / new http ("http://127.0.0.1:8080/examples/servlet/httpgameserver");

} Protected void destroyApp (Boolean Parm1) throws javax.microedition.midlet.midletStateChangeException {/ ** @ Todo Implement this javax.microedition.midlet.midlet Abstract method * /}

}

In summary, J2ME and J2EE all communications can continue to be developed with this craft, which can develop dynamically downloaded images \ map resources, etc., or use J2ME to manage the management of the database. Advanced development application.

转载请注明原文地址:https://www.9cbs.com/read-133013.html

New Post(0)