The post and get methods of Android network programming and server interaction

Source: Internet
Author: User
Tags readline

These two requests are implemented on Android, primarily using Apache's HTTP Library

The GET request is implemented as follows:

The code is as follows Copy Code
GET Request//////////////////
public void Getrequestweb (View v)
{
HttpGet httpget = new HttpGet ("http://www.pan-apps.com");
HttpClient client = new Defaulthttpclient ();
InputStream inputstream = null;
try {
HttpResponse = Client.execute (HttpGet);
Httpentity = Httpresponse.getentity ();
InputStream = Httpentity.getcontent ();
BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (InputStream));

String line = "";
String result = "";
while (line = Bufferedreader.readline ())!= null) {
result = result + line;
System.out.println (line);
}

SYSTEM.OUT.PRINTLN (result);

catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
finally{
try {
Inputstream.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}


The POST request is implemented as follows:

The code is as follows Copy Code

Post Request//////////
public void Postrequest (View v) throws Clientprotocolexception, IOException
{
POST request
HttpPost HttpPost = new HttpPost ("http://218.0.4.4:8080/esoa/login.do");

Construct POST request content
Namevaluepair NameValuePair1 = new Basicnamevaluepair ("username", "Linpan");
Namevaluepair NameValuePair2 = new Basicnamevaluepair ("Password", "123456");
list<namevaluepair> namevaluepairs = new arraylist<namevaluepair> ();
Namevaluepairs.add (NAMEVALUEPAIR1);
Namevaluepairs.add (NAMEVALUEPAIR2);
Httpentity httpentity = new urlencodedformentity (namevaluepairs);

Httppost.setentity (httpentity);

Perform a POST request
HttpClient httpclient = new Defaulthttpclient ();
HttpResponse = Httpclient.execute (HttpPost);

Read return Data
Httpentity = Httpresponse.getentity ();
InputStream inputstream = null;
InputStream = Httpentity.getcontent ();
BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (InputStream));
String line = "";
String result = "";
while (line = Bufferedreader.readline ())!= null)
{
result = result + line;
System.out.println (line);
}

Inputstream.close ();

}

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.