Invoke code instance based on the Java monthly shipping API _java

Source: Internet
Author: User
Tags urlencode stringbuffer

The example of this article for everyone to share the Java monthly Shipping API call code for your reference, the specific contents are as follows

Import Java.io.BufferedReader;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.net.URLEncoder;
Import Java.util.HashMap;
 
Import Java.util.Map;
 
Import Net.sf.json.JSONObject; /** * Constellation Horoscope Sample CODE-aggregation data * Online interface Documentation: HTTP://WWW.JUHE.CN/DOCS/58 **/public class Juhedemo {public static final String Def_c
  Hatset = "UTF-8";
  public static final int def_conn_timeout = 30000;
  public static final int def_read_timeout = 30000; public static String useragent = "mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/29.0.1547.66
 
  Safari/537.36 ";
 
  Configure the key public static final String Appkey = "*************************" that you requested;
    1. OP query public static void GetRequest1 () {String result =null; String url = "Http://web.juhe.cn:8080/constellation/getAll";//Request interface address MAP params = newHASHMAP ()//Request parameter Params.put ("key", appkey);//apply Appkey (Apply Detail page query) params.put ("Consname", "");//constellation name, such as: Aries par
      Ams.put ("type", "");//potential type: Today,tomorrow,week,nextweek,month,year try {result =net (URL, params, ' get ');
      Jsonobject object = Jsonobject.fromobject (result);
      if (Object.getint ("Error_code") ==0) {System.out.println (Object.get ("result"));
      }else{System.out.println (Object.get ("Error_code") + ":" +object.get ("Reason"));
    } catch (Exception e) {e.printstacktrace ();  } public static void Main (string[] args) {}/** * * @param strurl Request address * @param params request parameter * @param method Request Methods * @return Network Request String * @throws Exception */public static string net (string strurl, Map params
    , String method) throws Exception {HttpURLConnection conn = null;
    BufferedReader reader = null;
    String rs = null;
      try {stringbuffer sb = new StringBuffer (); if (Method==null | | methOd.equals ("get")) {strURL = strurl+ "?"
      +urlencode (params);
      URL url = new URL (strurl);
      conn = (httpurlconnection) url.openconnection ();
      if (Method==null | | method.equals ("get")) {Conn.setrequestmethod (' get ');
        }else{Conn.setrequestmethod ("POST");
      Conn.setdooutput (TRUE);
      } conn.setrequestproperty ("User-agent", useragent);
      Conn.setusecaches (FALSE);
      Conn.setconnecttimeout (def_conn_timeout);
      Conn.setreadtimeout (def_read_timeout);
      Conn.setinstancefollowredirects (FALSE);
      Conn.connect (); if (params!= null && method.equals ("POST")) {try {dataoutputstream out = new DataOutputStream (
            Conn.getoutputstream ());
        Out.writebytes (UrlEncode (params)); catch (Exception e) {//Todo:handle Exception}} InputStream is = Conn.getinputstream ()
      ;
    reader = new BufferedReader (new InputStreamReader (IS, def_chatset));  String strread = null;
      while ((Strread = Reader.readline ())!= null) {sb.append (strread);
    rs = sb.tostring ();
    catch (IOException e) {e.printstacktrace ();
      finally {if (reader!= null) {reader.close ();
      } if (conn!= null) {conn.disconnect ();
  } return RS; ///Convert MAP type to request parameter public static String UrlEncode (map<string,object>data) {StringBuilder sb = new Stringbui
    Lder (); For (Map.entry I:data.entryset ()) {try {sb.append (I.getkey ()). Append ("="). Append (Urlencoder.encode
      Value () + "", "UTF-8")). Append ("&");
      catch (Unsupportedencodingexception e) {e.printstacktrace ();
  } return sb.tostring ();

 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.