Interface Call class-fengkun

Source: Internet
Author: User
Tags convert net

/** * Interface Call class * * @author Fengkun * */public class Datautil {private static final String TAG = "Datautil.java";                Private static Final Boolean isdebug = true;    /* ****** course [Begin] ****** *//** course: Word */public static final int cate_word = 1;    /** Course: Word */public static final int cate_words = 2;    /** Course: Sentence */public static final int cate_sentence = 3;    /** Course: Article */public static final int cate_piece = 4; /* ****** course [End] ****** *//** * interface * @param ssuffix suffix (after question mark) * Content returned by @return interface */public s Tatic string Callinterface (String ssuffix) {string sresult = PostURL (<span style= "font-family:arial, Helvetica , Sans-serif; " > Domain </span><span style= "font-family:arial, Helvetica, Sans-serif;" > + "api.aspx" + ssuffix);</span> if (isdebug) log.i (TAG, "Tune interface:" + Const.namespace + "api.aspx?" + Ssuffi        x + "\ n return:" + Sresult);    return sresult; /** * Gets the page code specified by the parameter, returns it to the caller, and the callerParse * Returns String */public static string PostURL (string url) {InputStream is = null;        String result = "";            try {HttpClient HttpClient = new Defaulthttpclient ();            HttpPost HttpPost = new HttpPost (URL);            HttpResponse response = Httpclient.execute (HttpPost);            httpentity entity = response.getentity ();        is = Entity.getcontent ();            } catch (Exception e) {e.printstacktrace ();        Return "Fail to establish HTTP connection!" + e.tostring ();            } try {BufferedReader reader = new BufferedReader (new InputStreamReader (IS, "utf-8"));            StringBuilder sb = new StringBuilder ();            String line = null;            while (line = Reader.readline ())! = null) {Sb.append (line + "\ n");            } is.close ();        result = Sb.tostring ();           } catch (Exception e) {e.printstacktrace (); Return "Fail to convert net stream!";    } return result;     /** * Method of uploading files to server * @return True-sent successfully, false-send failed.        */public static string UploadFile (File ffile) {string end = "\ r \ n";        String Twohyphens = "--";        String boundary = "* * * * *";        StringBuffer Sbinput=null;            try {URL urls = new URL (domain name + "uploadtx.aspx");            HttpURLConnection con = (httpurlconnection) urls.openconnection ();            Allow input, Output, and do not use cache Con.setdoinput (TRUE);            Con.setdooutput (TRUE);            Con.setusecaches (FALSE);            Set the Method=post con.setrequestmethod ("POST") to be transmitted;            Setrequestproperty con.setrequestproperty ("Connection", "keep-alive");            Con.setrequestproperty ("Charset", "UTF-8");            Con.setrequestproperty ("Content-type", "multipart/form-data;boundary=" + boundary); Set DataOutputStream DataoutputstreAM ds = new DataOutputStream (Con.getoutputstream ());//Ds.writebytes (Twohyphens + boundary + end);//            Ds.writebytes ("content-disposition:form-data;" + "name=\" file1\ "; filename=\" "+ ffile.getname () +" \ "" + end);//            Ds.writebytes (end);            Get the file FileInputStream fileinputstream fstream = new FileInputStream (Ffile.getabsolutepath ());            Set each write to 1024bytes int buffersize = 1024;            byte[] buffer = new Byte[buffersize];            int length =-1; Reads data from a file to a buffer while (length = fstream.read (buffer))! =-1) {//writes data to Dataoutputstre            AM medium ds.write (buffer, 0, length);            }//ds.writebytes (end);//Ds.writebytes (Twohyphens + boundary + twohyphens + end);            Close streams fstream.close ();            Ds.flush ();            Get response content InputStream is = Con.getinputstream (); int ch;           Sbinput = new StringBuffer ();            while ((ch = is.read ())! =-1) {sbinput.append ((char) ch);            } log.e (TAG, "server returned:" + sbinput.tostring ());        Close DataOutputStream ds.close ();            } catch (Exception e) {e.printstacktrace ();        Return "Failed";    } return sbinput.tostring (); }    }
</pre> <pre name= "code" class= "java" >    /**     * Communication interface Information     *    /private void Callinterface () {        New Thread () {public            void run () {
<span style= "White-space:pre" ></span>//pass in the corresponding value according to different interface field requirements                String sinfo = Datautil.callinterface (" Menu=yd&tel= "+phonenumber+" &rs= "+peoplenumber+" &tp= "+repasttype";                Statusjudge (sinfo);                                int ihaoma = 0;   Row number                int idwrs = 0;    Number of equal digits                                //Parse out word list, Word scoring                try {                    jsonobject json = new Jsonobject (sinfo);
<span style= "White-space:pre" ></span>//JSON is the returned object, succeeded to ' success ', failed to ' failed '                    if (json.getstring (" Status "). Equals (" Success ")) {
<span style= "White-space:pre" ></span>//Get object information                        JSON = new Jsonobject (json.getstring ("message"));                                                Ihaoma = Json.getint ("Haoma");                        Idwrs = Json.getint ("Dwrs");                    }                } catch (Exception e) {                    e.printstacktrace ();                    Handler.sendemptymessage (Msg_no_netword);                    return;                }
<span style= "White-space:pre" ></SPAN>}
}
<pre name= "code" class= "Java" >



Interface Call class-fengkun

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.