android-Getting network data

Source: Internet
Author: User

Access to network data mainly by the transaction (or request, interface, etc.), and these transactions by the Java network communication, HttpURLConnection and httpclient implementation, the following is a specific example.

As we all know, network communication, send request There are two ways, get and post, here is no exception. 1.HttpURLConnection get the network data, get way to put the parameters in the URL after the passing of the past, and will be seen, generally not too safe, but the Get method only gets the data, does not update the data. Step: (1). Establish Url,url url=new URL (urltmp); Urltmp=http://96.0.57.141:8080/pmobile/noticeinfolistqry.do? Notetype=1 (2) Use a URL to establish a connection, HttpURLConnection urlcon=url.openconnection () (3) connection, and get the data stream InputStreamReader reader=new InputStreamReader (Urlcon.getinputstream ()); (4) using IO Stream processing data, here take BufferedReader as an example BufferedReader bf=new bufferedreader (reader); code example:


2. Use the Post method to get the data, Post mode parameters are placed in the content of the request sent, rather than in the URL, relatively safe, but the post has an updated operation, the general form form submitted data are the Post method   steps: (1) Create a URL, Same as Get mode URL url=new url (xxxxx);(2) Establish connection, HttpURLConnection urlcon=url.openconnection ();(3) configuration Properties, Here is the biggest difference urlcon.setdooutput (true); Does not set the Dooutput property to send data Urlcon.setdoinput (TRUE) on the content; This doinput property can not be set, the default is Trueurlcon.setrequestmethod ("POST"); Do not set this way can also get the data, and note that the post to all capital urlcon.setusecaches (false);//post mode can not use the cache, security issues? Urlcon.setinstancefollowredirects (true);// //If you do not set this property, it is possible to throw an exception when the argument that is sent is an object Urlcon.setrequestproperty (" Content-type "," Application/x-java-serialized-object "); Urlcon.connect ();  //connection must be above, or not, because the default connection when calling OutputStream   (4) writes to the contents of the feed using dataoutputstream arguments DataOutputStream data=new DataOutputStream (Urlcon.getoutputstream ()); String param= "notetype=1";d ata.writebytes (param);d ata.flush ();d ata.close ();  (5) Gets the data stream, Same as Get mode. InputStreamReader reader=new InputStreamReader (Urlcon.getinputstream ());  (6) Processing data BufferedReader bf= The new BufferedReader (reader);  code is as follows:


3. Use the Get method of httpclient to get the data, just put the parameters behind the URL to send the steps: (1). Create a GET request, HttpGet request=new httpget (XXX);(2). Create a HttpClient object, HttpClient clinet=new Defaulthttpclien ();(3). Call the client's Execute method, get the return data, HttpResponse Response=client.execute ( Request);(4). Gets data, String res=entityutils.tostring (Response.getentity ()); The code is as follows:

4. Use the HttpClient Post method to get the data, the parameters are placed in the requested content (1). New a HttpPost object, HttpPost request=new httppost (XXX);(2). Create parameter key-value pairs, Only use the prescribed format Namevaluepair,list param=new ArrayList ();(3). Add parameters, Param.add (new Basicnamevaluepair ("xxx", xxxx));(4). Convert the parameter format to httpentity entity=new urlencodeformentity (param, "UTF-8");(5). Place the parameter in the request, request.setentity (entity);(6). Create Httpclient,httpclient client=new defaulthttpclient ();(7). Perform the connection, get the return content, HttpResponse Response=client.execute ( Request);(8). Gets data, String res=entityutils.tostring (Response.getentity ());

android-Getting network data

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.