国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁化县| 友谊县| 突泉县| 滦南县| 高邑县| 社旗县| 大新县| 双柏县| 宁晋县| 铜鼓县| 义乌市| 夹江县| 隆昌县| 离岛区| 海阳市| 客服| 濮阳县| 安宁市| 石阡县| 祁东县| 长宁区| 古交市| 亳州市| 法库县| 斗六市| 美姑县| 九江县| 额济纳旗| 麟游县| 本溪市| 敖汉旗| 习水县| 昌图县| 高淳县| 三明市| 莫力| 云安县| 兴义市| 漳浦县| 呼图壁县| 金沙县|