Android's most original network request (HttpClient)

Source: Internet
Author: User

Eclipse may require Org.apache.http.legacy.jar, which can be found in the/optional directory of SDK/PLATFORMS/ANDROID-XX (the highest SDK version you installed)

1 //Network access Requests2 NewThread (NewRunnable () {3 4 @Override5      Public voidrun () {6 7String url = "";8         //Create Httpcient9HttpClient client =Newdefaulthttpclient ();Ten  One         //Create a POST request AHttpPost post =Newhttppost (URL); -list<namevaluepair> pairs =NewArraylist<namevaluepair>(); -         //Configure the request header thePost.addheader ("", "" "); -  -         //Configure request Parameters -Pairs.add (NewBasicnamevaluepair ("", "" ")); +Pairs.add (NewBasicnamevaluepair ("", "" ")); -  +         Try { APost.setentity (Newurlencodedformentity (pairs)); atHttpResponse response =Client.execute (POST); -  -             intStatusCode =response.getstatusline (). Getstatuscode (); -             if(StatusCode = = 200) { -                 //get the return result of the network -String result =entityutils.tostring (Response.getentity ()); in  -                 //UI action is required in the main thread to Toast.maketext (context, result, toast.length_short). Show (); +             } -}Catch(Exception e) { the             //Todo:handle Exception *         } $     }Panax Notoginseng}). Start ();

Android's most original network request (HttpClient)

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.