HttpClient androidStudio中的基础使用

来源:互联网 发布:linux怎么帮编写脚本 编辑:程序博客网 时间:2024/06/05 07:47
 modle的buil.gradle文件中android根标签下   添加useLibrary 'org.apache.http.legacy'/** * httpClient-- apche 基金会维护的请求网络的工具;工具类 */public class NetWorkUtils {    public  String tag = "NetWorkUtils";    /**     * apache     *     * @param jsonUrl     * @return     */    public String getJsonByHttpClientGet(String jsonUrl) {        //获取httpclient对象        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();        //准备一个get请求//        HttpGet httpGet = new HttpGet(jsonUrl);        HttpPost httpPost = new HttpPost(jsonUrl);        try {            //得到服务器返回的数据;            HttpResponse response = defaultHttpClient.execute(httpPost);            //得到状态码            int statusCode = response.getStatusLine().getStatusCode();            if(statusCode ==200){                //entiry 里面封装的数据;                HttpEntity entity = response.getEntity();                //这个result就是json字符串,剩下的就是解析工作了;                String result = EntityUtils.toString(entity);                Log.e(TAG, "result: "+result );            }        } catch (IOException e) {            e.printStackTrace();        }        return null;    }}
原创粉丝点击