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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿拉善左旗| 洛阳市| 丹阳市| 贵港市| 安顺市| 安宁市| 宣武区| 双鸭山市| 正蓝旗| 台南县| 浠水县| 巴林右旗| 玉林市| 巴东县| 新巴尔虎左旗| 井陉县| 静乐县| 高邑县| 习水县| 金川县| 淅川县| 沧州市| 敦煌市| 巫溪县| 镇坪县| 洞头县| 施秉县| 博野县| 宜春市| 洛隆县| 玉环县| 扬中市| 牙克石市| 运城市| 深圳市| 贵溪市| 澳门| 青川县| 宜川县| 竹山县| 和硕县|