HttpClient obtaining third-party interface data and parsing get JSON

Source: Internet
Author: User
Tags http post

First to the company internship, you need to get the JSON data returned by the third-party interface via HTTP POST and parse the JSON array to get the value

@RequestMapping ("/getproductname")
@ResponseBody
PublicArrayList Getproductname (HttpServletRequest request)throwsException {

HttpPost HttpPost =NewHttpPost ("");
Closeablehttpclient client = httpclients.Createdefault();
String respcontent =NULL;
Jsonobject Jsonparam =NewJsonobject ();
stringentity entity =NewStringentity (Jsonparam.tostring (),"Utf-8");//solve Chinese garbled problem
    Entity.setcontentencoding ("UTF-8");
Entity.setcontenttype ("Application/json");
Httppost.setentity (entity);
System. out. println ();
ArrayList productlist =NewArrayList ();

Try{
HttpResponse resp = Client.execute (HttpPost);
if(Resp.getstatusline (). Getstatuscode () = = 200) {
Httpentity he = resp.getentity ();
Respcontent = Entityutils.toString(He,"UTF-8");
System. out. println ("This section outputs third-party access data----------------> "+respcontent);
Jsonobject jsonobject= JSON.parseobject(respcontent);
String Jsonstr =jsonobject.getstring (" properties that need to be acquired in JSON ");//removedJSONOne of the properties in the array
            System. out. println ("This output returns the product name"+JSONSTR);
Jsonarray Jsonarray = Jsonobject.getjsonarray (" properties that need to be acquired in JSON ");
for(inti = 0;i<jsonarray.size (); i++) {
//Object list = Jsonarray.get (i);
//system.out.println (list);
            }

Jsonobject Jo = Jsonobject.parseobject(respcontent);
System. out. println ("=============="+JO);
Net.sf.json.JSONObject jsonobj = Net.sf.json.JSONObject.Fromobject(Jo);
Net.sf.json.JSONArray jsonArray1 = Jsonobj.getjsonarray ("Properties that need to be acquired in JSON");
for(intI=0;i<jsonarray1.size (); i++)
{
String productName = (string) jsonarray1.getjsonobject (i). Get ("One of the properties in a JSON array");
System. out. println (ProductName);
Productlist.add (ProductName);
}


}}Catch(Exception e) {

System. out. println ("An exception occurred to the docking third-party interface");
}

returnProductList;
}

HttpClient obtaining third-party interface data and parsing get JSON

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.