In Android, three common data submission methods are available for & quot; get & quot;, & quot; post & quot; requests.

Source: Internet
Author: User

There are three common data submission methods for "get" and "post" requests in Android.

I. Use HttpURLConnection to submit data

"Get" Request

Code:

String path = "http: // address? Data 1 name = "+ URLEncoder. encode (data 1," UTF-8 ") +" & Data 2 name = "+ URLEncoder. encode (Data 2," UTF-8 ");

URL url = new URL (path );

HttpURLConnection conn = (HttpURLConnection) url. openConnection ();

// Set the request method to uppercase.

Conn. setRequestMethod ("GET ");

Conn. setConnectTimeout (5000 );

Int code = conn. getResponseCode ();

If (code = 200 ){

InputStream is = conn. getInputStream ();

ByteArrayOutputStream baos = new ByteArrayOutputStream ();
Int len =-1;
Byte [] buffer = new byte [1024];
While (len = is. read (buffer ))! =-1 ){
Baos. write (buffer, 0, len );
}
Is. close ();

// Get the data returned by the server.
Result = baos. toString ();

}

 

 

"Post" Request

URL url = new URL (path );

HttpURLConnection conn = (HttpURLConnection) url. openConnection ();

// 1 set the request method to uppercase.

Conn. setRequestMethod ("POST ");

// Set the response time

Conn. setConnectTimeout (5000 );

// 2 set the http request data type to form

Conn. setRequestProperty ("Content-type", "application/x-www-form-urlencoded ");

String data = "data 1 name =" + URLEncoder. encode (data 1, "UTF-8") + "& Data 2 name =" + URLEncoder. encode (Data 2, "UTF-8 ");

// 3 set the length of data written to the server

Conn. setRequestProperty ("Content-Length", String. valueOf (data. length ()));

// 4 specify the data to be written to the server

Conn. setDoOutput (true );

// 5 start to write data to the server

Conn. getOutputStream (). write (data. getBytes );

Int code = conn. getResponseCode ();

If (code = 200 ){

InputStream is = conn. getInputStream ();

ByteArrayOutputStream baos = new ByteArrayOutputStream ();
Int len =-1;
Byte [] buffer = new byte [1024];
While (len = is. read (buffer ))! =-1 ){
Baos. write (buffer, 0, len );
}
Is. close ();

// Note: the reflux encoding is UTF-8 by default.

Result = baos. toString ();

}

Ii. Use HttpClient to submit data

Note: HttpClient is built into the Android SDK. You can directly use it without adding any additional jar packages. copy and paste the files from the com folder to the project.

Get method:

String path = "http: // address? Data 1 name = "+ URLEncoder. encode (data 1," UTF-8 ") +" & Data 2 Name "+ URLEncoder. encode (Data 2," UTF-8 ");

// The process can be understood as a user's browser operation.

// 1 open the browser

HttpClient client = new DefaultHttpClient ();

// 2 enter the address

HttpGet httpGet = new HttpGet (path );

// 3. Press enter.

HttpResponse response = client.exe cute (httpGet );

// Obtain the status code

Int code = response. getStatusLine (). getStatusCode ();

 

Post method:

String path = "http: // address ";

// 1 open the browser

HttpClient client = new DefaultHttpClient ();

// 2 enter the address

HttpPost httpPost = new HttpPost (path );

List <NameValuePair> parameters = new ArrayList <NameValuePair> ();

Parameters. add (new BasicNameValuePair ("data 1 Name", data 1 ));

Parameters. add (new BasicNameValuePair ("Data 2 Name", data 1 ));

HttpPost. setEntity (new UrlEncodedFormEntity (parameters, "UTF-8 "));

// 3. Press enter.

HttpResponse response = client.exe cute (httpPost );

// 4 get the status code

Int code = response. getStatusLine (). getStatusCode ();

 

Iii. Use AsyncHttpClient framework to submit data

The source code can be downloaded from the Internet.

Copy the source code in the src directory of the downloaded source code to the src directory of your project.

GET method:

// Request Path

String path = "http: // address? Data 1 name = "+ URLEncoder. encode (data 1) +" & Data 2 Name "+ URLEncoder. encode (Data 2 );

AsyncHttpClient client = new AsyncHttpClient ();

Client. get (path, new AsyncHttpResponseHandler (){

Public void onSuccess (int statusCode, Header [] headers, byte [] responseBody ){

// The request is successful.

New String (responseBody); // returned data

}

Public void onFailure (int statusCode, Header [] headers, byte [] responseBody, Throwable error ){

// Request failed

String (responseBody );

}

});

 

POST method:

String path = "http: // address ";

AsyncHttpClient client = new AsyncHttpClient ();

RequestParams params = new RequestParams ();

Params. put ("data 1 Name", data 1 );

Params. put ("Data 2 Name", data 2 );

Client. post (path, params, new AsyncHttpResponseHandler (){

Public void onSuccess (int statusCode, Header [] headers, byte [] responseBody ){

// The request is successful.

New String (responseBody); // returned data

}

Public void onFailure (int statusCode, Header [] headers, byte [] responseBody, Throwable error ){

// Request failed

String (responseBody );

}

});

 

The above is the basic code. If the code is not well written, we hope you will not be surprised. We also hope to communicate with you a lot.

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.