中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android的Http請(qǐng)求工具類

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
    import java.io.BufferedReader;  
    import java.io.ByteArrayOutputStream;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.InputStreamReader;  
    import java.io.PrintWriter;  
    import java.net.HttpURLConnection;  
    import java.net.URL;  
      
    //Http請(qǐng)求的工具類  
    public class HttpUtils  
    {  
      
        private static final int TIMEOUT_IN_MILLIONS = 5000;  
      
        public interface CallBack  
        {  
            void onRequestComplete(String result);  
        }  
      
      
        /** 
         * 異步的Get請(qǐng)求 
         *  
         * @param urlStr 
         * @param callBack 
         */  
        public static void doGetAsyn(final String urlStr, final CallBack callBack)  
        {  
            new Thread()  
            {  
                public void run()  
                {  
                    try  
                    {  
                        String result = doGet(urlStr);  
                        if (callBack != null)  
                        {  
                            callBack.onRequestComplete(result);  
                        }  
                    } catch (Exception e)  
                    {  
                        e.printStackTrace();  
                    }  
      
                };  
            }.start();  
        }  
      
        /** 
         * 異步的Post請(qǐng)求 
         * @param urlStr 
         * @param params 
         * @param callBack 
         * @throws Exception 
         */  
        public static void doPostAsyn(final String urlStr, final String params,  
                final CallBack callBack) throws Exception  
        {  
            new Thread()  
            {  
                public void run()  
                {  
                    try  
                    {  
                        String result = doPost(urlStr, params);  
                        if (callBack != null)  
                        {  
                            callBack.onRequestComplete(result);  
                        }  
                    } catch (Exception e)  
                    {  
                        e.printStackTrace();  
                    }  
      
                };  
            }.start();  
      
        }  
      
        /** 
         * Get請(qǐng)求,獲得返回?cái)?shù)據(jù) 
         *  
         * @param urlStr 
         * @return 
         * @throws Exception 
         */  
        public static String doGet(String urlStr)   
        {  
            URL url = null;  
            HttpURLConnection conn = null;  
            InputStream is = null;  
            ByteArrayOutputStream baos = null;  
            try  
            {  
                url = new URL(urlStr);  
                conn = (HttpURLConnection) url.openConnection();  
                conn.setReadTimeout(TIMEOUT_IN_MILLIONS);  
                conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);  
                conn.setRequestMethod("GET");  
                conn.setRequestProperty("accept", "*/*");  
                conn.setRequestProperty("connection", "Keep-Alive");  
                if (conn.getResponseCode() == 200)  
                {  
                    is = conn.getInputStream();  
                    baos = new ByteArrayOutputStream();  
                    int len = -1;  
                    byte[] buf = new byte[128];  
      
                    while ((len = is.read(buf)) != -1)  
                    {  
                        baos.write(buf, 0, len);  
                    }  
                    baos.flush();  
                    return baos.toString();  
                } else  
                {  
                    throw new RuntimeException(" responseCode is not 200 ... ");  
                }  
      
            } catch (Exception e)  
            {  
                e.printStackTrace();  
            } finally  
            {  
                try  
                {  
                    if (is != null)  
                        is.close();  
                } catch (IOException e)  
                {  
                }  
                try  
                {  
                    if (baos != null)  
                        baos.close();  
                } catch (IOException e)  
                {  
                }  
                conn.disconnect();  
            }  
              
            return null ;  
      
        }  
      
        /**  
         * 向指定 URL 發(fā)送POST方法的請(qǐng)求  
         *   
         * @param url  
         *            發(fā)送請(qǐng)求的 URL  
         * @param param  
         *            請(qǐng)求參數(shù),請(qǐng)求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。  
         * @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果  
         * @throws Exception  
         */  
        public static String doPost(String url, String param)   
        {  
            PrintWriter out = null;  
            BufferedReader in = null;  
            String result = "";  
            try  
            {  
                URL realUrl = new URL(url);  
                // 打開和URL之間的連接  
                HttpURLConnection conn = (HttpURLConnection) realUrl  
                        .openConnection();  
                // 設(shè)置通用的請(qǐng)求屬性  
                conn.setRequestProperty("accept", "*/*");  
                conn.setRequestProperty("connection", "Keep-Alive");  
                conn.setRequestMethod("POST");  
                conn.setRequestProperty("Content-Type",  
                        "application/x-www-form-urlencoded");  
                conn.setRequestProperty("charset", "utf-8");  
                conn.setUseCaches(false);  
                // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行  
                conn.setDoOutput(true);  
                conn.setDoInput(true);  
                conn.setReadTimeout(TIMEOUT_IN_MILLIONS);  
                conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);  
      
                if (param != null && !param.trim().equals(""))  
                {  
                    // 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流  
                    out = new PrintWriter(conn.getOutputStream());  
                    // 發(fā)送請(qǐng)求參數(shù)  
                    out.print(param);  
                    // flush輸出流的緩沖  
                    out.flush();  
                }  
                // 定義BufferedReader輸入流來讀取URL的響應(yīng)  
                in = new BufferedReader(  
                        new InputStreamReader(conn.getInputStream()));  
                String line;  
                while ((line = in.readLine()) != null)  
                {  
                    result += line;  
                }  
            } catch (Exception e)  
            {  
                e.printStackTrace();  
            }  
            // 使用finally塊來關(guān)閉輸出流、輸入流  
            finally  
            {  
                try  
                {  
                    if (out != null)  
                    {  
                        out.close();  
                    }  
                    if (in != null)  
                    {  
                        in.close();  
                    }  
                } catch (IOException ex)  
                {  
                    ex.printStackTrace();  
                }  
            }  
            return result;  
        }  
    }  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android 判斷SD卡是否存在及容量查詢

下一篇:java md5工具類