Zhao Yazhi _android_ Network Operation Tool Class

Source: Internet
Author: User

Package Com.cards.basic.util;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Android.content.context;import Android.net.connectivitymanager;import Android.net.networkinfo;import com.cards.commom.common;/** * @explaination Network Operation Tool Class * @author Yazhizhao * @time 2014-7-7 pm 3:35:32 */public class Httputil {private static httputil instance;private Ht Tpurlconnection conn;private inputstream is;private outputstream os;public static Httputil getInstance (context context) {if (instance = = null) {instance = new Httputil (context);} return instance;} Private final static int TIMEOUT = 30 * 1000;/** * Log tool label */private static String tag = "Httputil";/** * Contextual Context */private C Ontext context;/** * Construction Method * * @param context * Contextual environment */public Httputil (contexts context) {This.context = Contex t;} /** * * @explaination service sends an HTTP request * @auThor Yazhizhao * @time 2014-7-7 pm 3:36:36 * @param urlpath * @return * @throws Exception */public String postaccessserver (S Tring URLPath, String requestjsonstr) {Common.log ("url =" + URLPath); Common.log ("requestjsonstr =" + requestjsonstr); String result = null; URL url = null;conn = Null;os = null;try {url = new URL (urlpath), if (conn = = NULL) {conn = (httpurlconnection) Url.opencon Nection ();} Conn.setconnecttimeout (timeout); Conn.setreadtimeout (timeout); Conn.setdoinput (true); Conn.setdooutput (true); Conn.setrequestmethod ("POST"); Conn.setusecaches (false); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("Charset", "UTF-8"), if (requestjsonstr! = null) {byte[] dataupdate = Requestjsonstr.getbytes (" UTF-8 "); OS = Conn.getoutputstream (); Os.write (dataupdate);} Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); is = Conn.getinputstream (); byte[] temp = new Byte[256];int len = 0;while (len = is.read (temp))! =-1) {baos.write (temp, 0, len);} byte[] Dataresult = Baos.tobyTearray (); Common.log ("Data Length =" + dataresult.length); if (Dataresult! = NULL | |! "". Equals (Dataresult)) {result = new String (Dataresult, "UTF-8");} Is.close (); is = null;if (OS! = null) {os.close (); OS = null;} conn = Null;url = null;} catch (Malformedurlexception e) {Common.log ("malformedurlexception =" + e.tostring ());} catch (IOException e) {Common.log ("IOException =" + e.tostring ());} catch (Exception e) {Common.log ("Exception =" + e.tostring ());} Common.log ("result =" + result); Common.log ("Stop at Httputil"); return result;} /** * * @explaination determine if the network is available * @author Yazhizhao * @time 2014-7-7 pm 3:37:20 * @param context * @return */public static B Oolean isnetworkavailable (Context context) {Boolean flag = false; Connectivitymanager Localconnectivitymanager = (Connectivitymanager) context.getapplicationcontext (). Getsystemservice (Context.connectivity_service); if (Localconnectivitymanager! = null) {try {Networkinfo Localnetworkinfo = Localconnectivitymanager.getactivenetworkinfo (); if ((Localnetworkinfo = = null) | | (!localnetworkinfo.isavailable ())) Flag = false;//Unavailable Elseflag = true;//available} catch (Exception e) {e.printstacktrace (); flag = false;}}   return flag;} /** * Force Disconnect * * @return */public boolean killconnection () {try {if (is! = null) {Is.close (); is = null;} if (OS! = null) {os.close (); OS = null;} IF (conn! = null) {conn.disconnect (); conn = null;}} catch (Exception e) {return false;} return true;}}

Related Article

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.