Android Network Programming uses HttpClient to access the web site

Source: Internet
Author: User

Android Network Programming uses HttpClient to access the web site

The HttpClientDemo. java Interface contains two buttons and a text box.

/** Use HttpClientlai to submit access requests, receive responses * A, send GET request * 1, create HttpClient object; HttpClient httpclient = new DefaultHttpClient (); * 2, send GET request, create HttpGet object: HttpGet httpget = new HttpGet ("http://www.baidu.com"); * 3, using the HttpClient object to implement the HttpGet object will get the server response object HttpResponse object, the response is encapsulated in HttpResponse: * HttpResponse httpresponse=httpclient.exe cute (httpget); * 4. Obtain the Http instance HttpEntity entity = httpresponse from the httpresponse response. getEntity (); **/publi C class HttpClientDemo extends Activity {TextView response; // declare the HttpClient object HttpClient httpclient; Handler handler = new Handler () {public void handleMessage (Message msg) {if (msg. what = 0x123) {// use response to display the response of the server. append (msg. obj. toString () + "\ n") ;}};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_http _ Client); // 1. Create the DefaultHttpClient object. The HttpClient interface callback is an interface httpclient = new DefaultHttpClient (); response = (TextView) findViewById (R. id. response);}/** send GET request process to the service ***/public void accessSecret (View v) {response. setText (""); // click the button to enable the Thread and send the Get request new Thread () {public void run () {// 2, create an HttpGet object HttpGet httpget = new HttpGet ("http: // localhost: 8080/foo/secret. jsp "); // deploy jsp on To cat server try {// 3, use HttpC When the lient object implements the HttpGet object, the server will get the HttpResponse response object, and the response will be encapsulated in HttpResponse httpresponse=httpclient.exe cute (httpget); // 4, obtain the Http instance HttpEntity entity = httpresponse from the httpresponse response. getEntity (); if (entity! = Null) {// 5. The entity instance obtains the content, establishes an input stream, and reads the server content BufferedReader br = new BufferedReader (new InputStreamReader (entity. getContent (); String line = null; while (line = br. readLine ())! = Null) {// cyclically read content from the input stream Message msg = new Message (); msg. what = 0x123; msg. obj = line; handler. sendMessage (msg); // send it to the UI thread to update the UI component }}catch (ClientProtocolException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}. start ();}/** Post request process ***/public void showLogin (View v) {final View loginDialog = getLayoutInflater (). inflate (R. layout. login, null); new AlertDialog. builder (HttpClientDemo. this ). setTitle ("log on to the system "). setView (loginDialog ). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// obtain the username and password of the dialog box final String name = (EditText) loginDialog. findViewById (R. id. name )). getText (). toString (); final String pass = (EditText) loginDialog. findViewById (R. id. pass )). getText (). toString (); // Click OK to enable the Thread. The Thread sends the Post request new Thread () {public void run () {try {// 2, create an HttpPost object HttpPost httppost = new HttpPost ("http: // localhost: 8080/foo/login. jsp "); // jsp is deployed on the To cat server // 3. It encapsulates the passed parameters. NameValuePair is a List of simple name values on the node type.
 
  
Params = new ArrayList
  
   
(); Params. add (new BasicNameValuePair ("name", name); // add the params parameter. add (new BasicNameValuePair ("pass", pass); // 3, sets the encoding httppost. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8); // 4, The HttpClient object executes the HttpPost request to obtain the HttpResponse httpresponse#httpclient.exe cute (httppost); // 5, if the status code is 200, the server is successful. if (httpresponse. getStatusLine (). getStatusCode () = 200) {// 200: Response successful, 301/302: Redirection, 404: not found: no resource found, 501 server encountered an error, make it unable to provide service to the request String msg = EntityUtils. toString (httpresponse. getEntity (); loiter. prepare (); // prompt that the logon is successful Toast. makeText (HttpClientDemo. this, msg, Toast. LENGTH_LONG ). show (); logoff. loop () ;}} catch (ParseException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}. start ();}}). setNegativeButton ("cancel", null ). show ();}
  
 

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.