HttpClient androidStudio中的基础使用

 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;


    }
}




猜你喜欢

转载自blog.csdn.net/hubianyu/article/details/78410051