Network Programming (i)

Source: Internet
Author: User

Get method Instance:
    String url= "Http://202.194.14.215/iactivity/mobile/msg.php?act=1"; String tixingfanhuizhi=null;defaulthttpclient client=new defaulthttpclient ();//New Clienthttpget get=new HttpGet (URL) ; New Httpgethttpresponse Response=null;get.setheader ("Cookie", GetCookie ());//setcookie, sometimes you may not be able to use response= Client.execute (get); Tixingfanhuizhi = Entityutils.tostring (Response.getentity (), "UTF-8");D ocument Doc=jsoup.parse (Tixingfanhuizhi);//Generate a document file with a string return value, and then you can parse the HTML file with Jsoup elements pinglun=doc.select ("div"); int a= Pinglun.size ();

Post Method Instance
Defaulthttpclient httpclient = new Defaulthttpclient ();//create httpclientlist<namevaluepair> params = new ArrayList <NameValuePair> ();p Arams.add (New Basicnamevaluepair ("User", username));p arams.add (New Basicnamevaluepair (" Key ", key)); urlencodedformentity entity = null;try {entity = new urlencodedformentity (params, "UTF-8"); HttpPost HttpPost = new HttpPost (URL); httppost.setentity (entity); Using the Post method, the inside place parameter try {response = Httpclient.execute (HttpPost);//Get Httpresponseheader Header = Response.getfirstheader ("Set-cookie"); if (header = null) {HttpCookie = Header.getvalue ();} try {Fanhuishuzhi = entityutils.tostring (Response.getentity (), HTTP. UTF_8); Get the contents of code = Integer.parseint (Fanhuishuzhi);//Convert to data value} catch (ParseException e) {//TODO auto-generated catch Blocke.printstacktrace (); Toast.maketext (Getapplicationcontext (), Getresources (). getString (R.string.login_no), Toast.length_short). Show (); Dialog.dismiss ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktraCE (); Toast.maketext (Getapplicationcontext (), Getresources (). getString (R.string.login_no), Toast.length_short). Show (); Dialog.dismiss ();}} catch (Clientprotocolexception e) {//TODO auto-generated catch Blocke.printstacktrace (); Toast.maketext (Getapplicationcontext (), Getresources (). getString (R.string.login_no), Toast.length_short). Show (); Dialog.dismiss ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (Unsupportedencodingexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}

Network Programming (i)

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.