Android Access Server, android access
Cocos2dx has been around for a while, and it has been around for android. I have never had time to write down my work experience. I will take some time to write down my work experience today.
How to obtain data from the android Access Server:
The transmission protocol uses the http Communication mode. This time, POST is used. The http pathname is stored in the variable cmd. The Protocol content format is json.
1 String urlStr = "http://219.234.95.44:6789" + cmd;//"10.1.30.220"; 2 // urlStr = urlStr + "/getName"; 3 // urlStr = "http://www.baidu.com/"; 4 URL url = null; 5 String result = null; 6 try { 7 url = new URL(urlStr); 8 HttpURLConnection urlConn = (HttpURLConnection)url.openConnection(); 9 urlConn.setDoInput(true);10 urlConn.setDoOutput(true);11 urlConn.setRequestMethod("POST");12 urlConn.setUseCaches(false);13 // urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");14 urlConn.setRequestProperty("Content-Type", "application/x-java-serialized-object");15 urlConn.setRequestProperty("Charset", "utf-8"); 16 urlConn.setConnectTimeout(20000);17 urlConn.connect();18 19 DataOutputStream dop = new DataOutputStream(urlConn.getOutputStream());20 if(param != null)21 dop.writeBytes(param);22 dop.flush();23 dop.close();24 25 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));26 27 String line = null;28 while((line = bufferedReader.readLine()) != null)29 {30 System.out.println(line);31 result = (result == null) ? line : result + line;32 }33 bufferedReader.close();34 urlConn.disconnect(); 35 } catch (MalformedURLException e) {36 // TODO Auto-generated catch block37 e.printStackTrace();38 } catch (IOException e) {39 // TODO Auto-generated catch block40 e.printStackTrace();41 }42 43 Message msg = new Message(); 44 Bundle data = new Bundle(); 45 data.putString("cmd", cmd);46 data.putString("result", result); 47 msg.setData(data); 48 handler.sendMessage(msg);
Do not write this function in the main interface thread, so it is easy to report exceptions. The third behavior test code.
Check whether the server is running normally. You can use the command line Telnet function to open the command line and enter Telnet. If this is not an internal command, open the control panel and select a program, select to enable or disable the Windows function. In the displayed dialog box, check the "Telnet client" option and click "OK. Then you can use the Telnet command in the command line.
After the input is complete, enter "open 219.234.95.44 6789". The subsequent port number cannot be omitted, and a colon cannot be added before. Press enter. If no error is displayed, the server is normal. This code is relatively simple and will not be commented out here.
Rows 19th to 23rd are the parameters for sending data to the server, and rows 25th are the data that sends the request and returns the server response.
[Help] How does the android system simply access the server?
In fact, it is the problem of communication between the server and the client. If you access the server through http, the server must have serlvet to respond to your request. Similarly, you must use socket to request a connection to the server, then the server must have a program to listen to the connection request and then process it. The simplest problem is socket. However, the server must also have the listening Request Code. Thank you for your adoption.
How does android access the server?
Http post/http get
There are classes for accessing network android
You can look at the android network.