This piece of article from Http://blog.csdn.net/andywuchuanlong, reproduced please explain the source, thank you!
This article will be about how to choose the mobile communication channel in the project development, this so-called communication refers to the mobile phone terminal and the server data exchange, and the channel refers to the mobile phone terminal and the server for data exchange method.
As an example of Android project development, there are several types of communication tools available in Android: HttpURLConnection, HttpClient, sockets, and so on. But are these tools available for all channels?
There are several communication channels for mobile phones:
1, WLAN: Wireless network channels, such as WiFi, when the mobile phone using this channel way to the Internet, will directly request the server.
2, APN: Mobile APN access Point, is the base station, and this access point way there are probably two kinds:
A) WAP mode: This is a regional channel way, mainly for value-added services, such as China Mobile's proxy IP is 10.0.0.172, port is 80, the telecommunications port is 200. This way the Internet, through the WAP Gateway (proxy), the operator will first intercept, filter out the user's request, and then turn to the real resource server. More steps than usual, it also reduces the speed of the Internet. Go WAP gateway Not all communication tools are available. HttpURLConnection seems to be no, network stability is very poor. If you use WAP on your mobile phone, you need to configure it in your code.
b) NET method, which requests the server directly.
The following shows how the code chooses the communication channel.
First of all, what kind of communication channel do you decide to walk? is WiFi or APN,APN in the WAP special access network type: WiFi or Mobile, determine which APN is selected, get the APN proxy information public static Boolean checknet (Context con Text) {//judgment: WiFi link Boolean iswifi = iswificonnection (context); Judge: Mobile link Boolean ismobile = ismobileconnection (context); If mobile is in the link, determine which APN is selected if (ismobile) {//APN is selected, whether the agent information has content, if there is a WAP mode READAPN (context);//Determine which APN is selected} if (!iswifi &&!ismobile) { return false; } return true; }/** * APN is selected, the agent information is content, if there is a WAP way * * @param context */PR ivate static void Readapn (context context) {Uri Preferred_apn_uri = Uri.parse ("Content://telephony/carriers /PREFERAPN ");//4.0 simulator screen out this permission//operation contact similar to contentresolver resolver = Context.getconTentresolver (); Determine which APN is selected by cursor cursor = resolver.query (Preferred_apn_uri, NULL, NULL, NULL, or NULL); if (Cursor!=null&&cursor.movetofirst ()) {globalparams.proxy=cursor.getstring (cursor.getColumnInd EX ("proxy")); Globalparams.port=cursor.getint (Cursor.getcolumnindex ("PORT")); } }
HttpClient: Set proxy information public Httpclientutil () { client = new Defaulthttpclient (); Determine if you need to set proxy information if (Stringutils.isnotblank (Globalparams.proxy)) { //Set proxy information httphost host = new Httphost ( Globalparams.proxy, globalparams.port); Client.getparams (). Setparameter (Connroutepnames.default_proxy, host); } }
The application of mobile network communication channel in development