It's been a while since I've been doing cocos2dx, and I've been in touch with Android for quite some time. Have no time to write down their work experience, today take some time to write down a paragraph.
about how the Android access server obtains data:
The transport protocol uses the HTTP communication method, this time uses the post. The pathname of the HTTP protocol is stored in the variable cmd, and the content format of the protocol is JSON.
1String urlstr = "http://219.234.95.44:6789" + cmd;//"10.1.30.220";2 //urlstr = urlstr + "/getname";3 //urlstr = "http://www.baidu.com/";4URL url =NULL;5String result =NULL;6 Try {7URL =NewURL (URLSTR);8HttpURLConnection Urlconn =(HttpURLConnection) url.openconnection ();9Urlconn.setdoinput (true);TenUrlconn.setdooutput (true); OneUrlconn.setrequestmethod ("POST"); AUrlconn.setusecaches (false); - //urlconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); -Urlconn.setrequestproperty ("Content-type", "Application/x-java-serialized-object"); theUrlconn.setrequestproperty ("Charset", "Utf-8"); -Urlconn.setconnecttimeout (20000); - Urlconn.connect (); - +DataOutputStream DOP =NewDataOutputStream (Urlconn.getoutputstream ()); - if(param! =NULL) + dop.writebytes (param); A Dop.flush (); at dop.close (); - -BufferedReader BufferedReader =NewBufferedReader (NewInputStreamReader (Urlconn.getinputstream ())); - -String line =NULL; - while(line = Bufferedreader.readline ())! =NULL) in { - System.out.println (line); toresult = (Result = =NULL) ? Line:result +Line ; + } - bufferedreader.close (); the Urlconn.disconnect (); *}Catch(malformedurlexception e) { $ //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); -}Catch(IOException e) { the //TODO auto-generated Catch block + e.printstacktrace (); A } the +Message msg =NewMessage (); -Bundle data =NewBundle (); $Data.putstring ("cmd", cmd); $Data.putstring ("Result", result); - msg.setdata (data); -Handler.sendmessage (msg);
This section of the function is not written in the main interface thread, easy to report exceptions. The third behavior tests the code.
To detect if the server is working properly, you can use the command line Telnet feature, open the command line to enter Telnet, if prompted that this is not an internal command, open Control Panel, select the program, choose to turn Windows features on or off, in the popup dialog box to view the "Telnet client", And tick on, click OK to close. You can then use the Telnet command on the command line.
Enter "Open 219.234.95.44 6789" after the input is complete, and the trailing port number cannot be omitted and preceded by a colon. Then enter, if there is no prompt error, stating that the server is OK. This code is relatively simple and is not annotated here.
Rows 19th through 23rd are sets of parameters to send data to the server, and line 25th is the data that sends the request and returns the server answer.