国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 中HttpURLConnection與HttpClient使用的簡單實例

2020-04-11 12:02:21
字體:
來源:轉載
供稿:網友

1:HttpHelper.java

復制代碼 代碼如下:

public class HttpHelper {
    //1:標準的Java接口
    public static String getStringFromNet1(String param){
        String result="";
        try{
            URL url=new URL(param);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection();
            if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
                InputStream is=conn.getInputStream();
                byte[]data=new byte[1024];
                int len=is.read(data);
                result=new String(data,0,len);
                is.close();
                conn.disconnect();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    //2:Apache接口
    public static String getStringFromNet2(String param){
        String result="";
        try{
            HttpClient client=new DefaultHttpClient();
            HttpGet get=new HttpGet(param);
            HttpResponse response=client.execute(get);
            if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
                result=EntityUtils.toString(response.getEntity());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙江县| 福海县| 湘潭县| 宜城市| 秦皇岛市| 大田县| 固原市| 抚州市| 金溪县| 卢湾区| 林口县| 金寨县| 公安县| 延吉市| 青阳县| 湾仔区| 砚山县| 鄱阳县| 会昌县| 大兴区| 文山县| 望都县| 舞阳县| 龙山县| 洛川县| 富川| 五大连池市| 麟游县| 天津市| 玉门市| 奎屯市| 南宁市| 江永县| 彰武县| 达拉特旗| 宜昌市| 昌都县| 锦州市| 清苑县| 凤山市| 凤山市|