Java requests JSON data from the network and parses JSON data----(self-creation, please specify)

Source: Internet
Author: User

JSON data is more commonly used data type resolution, the advantages are not much to say. Take a look at the method:


public static Jsonobject getjsonobject (String url) {Jsonobject jsonobject = null;try {HttpClient HttpClient = new Defaulth Ttpclient (); HttpGet HttpGet       = new HttpGet (URL); Httpparams httpparams = Httpclient.getparams (); Httpconnectionparams.setconnectiontimeout (Httpparams, 5000); HttpResponse response = Httpclient.execute (HttpGet); StringBuilder builder = new StringBuilder (); BufferedReader BufferedReader = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent (), "Utf-8") ); for (String s = bufferedreader.readline (); s! = null; s = Bufferedreader.readline ()) {builder.append (s);} Jsonobject = new Jsonobject (builder.tostring ());} catch (Exception e) {e.printstacktrace (); jsonobject = null;} return jsonobject;}

The returned type is the Jsonobject type, and the subsequent addition of its own operation is possible.


Note: Depending on the amount of data requested, it may be time-consuming to use a thread to supportit, and you can use Asynctask with the following methods:

Take a look at my other blog post: Two ways to use Asynctask (with code)

How to get the JSON data to use it, you can refer to: JSON Learning note gson parsing JSON data (convenient, fast, including code)


Java requests JSON data from the network and parses JSON data----(self-creation, please specify)

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.