Summary of using httpclient in Android

Source: Internet
Author: User
Android can also open remote web pages and use httpclient

Get the page content. The following code is available in both methods:

Import java. Io. bufferedinputstream;

Import java. Io. bufferedreader;

Import java. Io. inputstream;

Import java. Io. inputstreamreader;

Import java.net. url;

Import java.net. urlconnection;

Import org. Apache. http. httpentity;

Import org. Apache. http. httpresponse;

Import org. Apache. http. Client. Methods. httpget;

Import org. Apache. http. Client. Methods. httpurirequest;

Import org. Apache. http. impl. Client. defaulthttpclient;

Import org. Apache. http. util. bytearraybuffer;

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. widget. textview;

Public class androidtelenet extends activity {

/** Called when the activity is first created .*/

Httpurirequest request = NULL;

Httpresponse resp = NULL;

Inputstream is = NULL;

Defaulthttpclient client = new defaulthttpclient ();

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

/* We will show the data we read in a textview .*/

Textview TV = new textview (this );

/* Will be filled and displayed later .*/

Stringbuffer mystring = new stringbuffer ();

String tips = NULL;

Try {

/* Define the URL we want to load data from .*/

// URL myurl = new URL (

// "Http: // 10.0.2.2: 8085/test/index. Action ");

/* Open a connection to that URL .*/

// Urlconnection ucon = myurl. openconnection ();

/*

* Define inputstreams to read from the urlconnection.

*/

// Inputstream is = ucon. getinputstream ();

// Bufferedinputstream Bis = new bufferedinputstream (is );

// If (bis. Read ()> 0)

//{

// Tips = "connecting to webpages ";

//}

// Else

//{

// Tips = "cannot connect to webpage ";

//}

/*

* Read bytes to the buffer until there is nothing more to read (-1 ).

*/

// Bytearraybuffer BAF = new bytearraybuffer (50 );

// Int current = 0;

// While (current = bis. Read ())! =-1 ){

// BAF. append (byte) Current );

//}

/* Convert the bytes read to a string .*/

// Mystring = new string (BAF. tobytearray ());

Request = new httpget ("http: // 10.0.2.2: 8085/test/index. Action ");

Resp=client.exe cute (request );

Httpentity entity = resp. getentity ();

Inputstream is = entity. getcontent ();

Bufferedreader BR = new bufferedreader (

New inputstreamreader (is, "gb2312 "));

String data = "";

While (Data = Br. Readline ())! = NULL ){

Mystring. append (data );

}

String result = mystring. tostring ();

System. Out. println ("result is" + result );

System. Out. println ("mystring" + mystring );

If (mystring. indexof ("sorry, Operation error or system maintenance")> 0)

{

Tips = "Tomcat startup, database, and other exceptions ";

}

Else

{

Tips = "normal, no exception information ";

}

} Catch (exception e ){

/* On any error we want to display it .*/

Tips = E. getmessage ();

}

/* Show the string on the GUI .*/

TV. settext (TIPS );

This. setcontentview (TV );

}

}

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.