(Android practice system II) Android interactive network transmission solution selection and implementation

Source: Internet
Author: User

Preface:

As described above, the implementation of login and account registration based on the dialog box is not detailed about the interaction between Android terminals and Internet services,

In the following section, I will describe the selection and analysis process of the scheme for selecting Android transmission, and define and implement the login and registration interfaces.

1 androidTransmission scheme analysis and selection

When talking about the android transmission scheme, what we generally think of is
Socket transmission, http-based web Services, and direct http post and get methods.

Next 【Network implementation conditions],【Scope of use],【Transmission advantages and disadvantages],[Data Transmission Format ],[Integrated development cost] and [Application complexity]Analysis of the Three transmission modes as the selection criteria.

The following is my analysis of these three transmission modes.

SocketTransmission

WebService

Post, getHow to obtain data

Network implementation conditions

Port: Specific Port

Protocol: TCP, UDP

(Currently, the non-80 port support for wireless networks is low. Currently, only wap and http protocols are supported)

Port 80

The Protocol is http

Port 80

The Protocol is http

Scope of use

Transmission with large data volume, simple format, and high real-time requirements

Standard Web service interfaces, fixed Interfaces

Http format, Fixed Interface

Advantages and disadvantages of transmission

Fast and small data volume

Average speed, large data volume

Moderate speed, moderate data volume

Transmission Format

Custom Data Format

SOAP

Json

Development Requirements

Client and server: Data splicing, parsing, and complete data verification

High Technical Requirements for developers

Client: Use third-party web service components to splice parameters and parse feedback data. The process is complex.

Server: Create a web Service

Technical requirements of developers: General

Client: Directly call the json package of android to parse json data. Simple process

Server: Generate the httpHandler-based ashx file Interface

Technical requirements of developers: low

Overall Cost

High

Average

Low

Based on the above analysis: select the transmission mode of json + httphandler

2Login and registration Interfaces

2.1Login interface implementation

Interface Definition:

Http: // 192.168.1.1/gointel/UserHandler. ashx? Action = login & Account = xuwenbing & PassWord = xuwenbing

Json format:

{ActionResult: false, Reason: "" account and password are inconsistent ""}

Source code:

 

View Code

/* User Login
* The server checks whether the user's account and password are consistent and returns results
**/
Public static Boolean Login (String Account, String PassWord)
{
// Step One gets the matching information of the current account and password from the server interface
Boolean actionResult = false;
String httpUrl = "http: // 221.181.127.43/gointel/UserHandler. ashx? Action = login & Account = "+ Account +" & PassWord = "+ PassWord;
// HttpGet connection object
HttpGet httpRequest = new HttpGet (httpUrl );

Try
{
// Obtain the HttpClinet object
HttpClient httpclient = new DefaultHttpClient ();

// Request HttpClient to obtain HttpResponse
HttpResponse httpResponse=httpclient.exe cute (httpRequest );

// The request is successful.
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK)
{
// Obtain the returned string
String strResult = EntityUtils. toString (httpResponse. getEntity ());

JSONObject jsonObject = new JSONObject (strResult );
// Obtain the returned value and determine whether it is correct
ActionResult = jsonObject. getBoolean ("ActionResult ");
}
}
Catch (Exception e)
{
Return false;

}
Return actionResult;
}


 

2.2Registration interface implementation

 Interface Definition:

Http: // 192.168.1.1/gointel/UserHandler. ashx? Action = register & Account = xuwenbing & PassWord = xxuwenbin & NiceName = ninilan;

Json format:

{ActionResult: false, Reason: "" the user name already exists ""}

Source code:

View Code

Public static Boolean Register (String account, String passWord,
String niceName ){

Boolean actionResult = false;
String httpUrl = "http: // 221.181.127.43/gointel/UserHandler. ashx? Action = register & Account = "+ account +" & PassWord = "+ passWord +" & NiceName = "+ niceName;
// HttpGet connection object
HttpGet httpRequest = new HttpGet (httpUrl );

Try
{
// Obtain the HttpClinet object
HttpClient httpclient = new DefaultHttpClient ();

// Request HttpClient to obtain HttpResponse
HttpResponse httpResponse=httpclient.exe cute (httpRequest );

// The request is successful.
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK)
{
// Obtain the returned string
String strResult = EntityUtils. toString (httpResponse. getEntity ());

JSONObject jsonObject = new JSONObject (strResult );

// Obtain the returned value and determine whether it is correct
ActionResult = jsonObject. getBoolean ("ActionResult ");
}
}
Catch (Exception e)
{
Return false;

}
Return actionResult;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.