HttpClient在Android Studio中使用

来源:互联网 发布:csgo mac版 编辑:程序博客网 时间:2024/05/21 17:23
build.gradle中写入
android {

useLibrary 'org.apache.http.legacy'

}

代码

//因为请求的是https协议的网址,eclipse下使用HttpGet请求会报错,需要添加以下这行代码
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());

//1.创建HttpClientHttpClient hc=new DefaultHttpClient();//2.创建HttpGet对象HttpGet hg = new HttpGet(strings[0]);//3.执行请求try {    HttpResponse response = hc.execute(hg);    //4.得到结果吗    int code = response.getStatusLine().getStatusCode();    if(code==200){        //5.得到结果数据        HttpEntity result = response.getEntity();        String string = EntityUtils.toString(result);        Log.e("TTTTTT",string+"++++++++++++++tttt");        return string;    }} catch (IOException e) {    e.printStackTrace();}

原创粉丝点击