我该怎么做才能在android studio中使用httpclient方法。

时间:2016-05-18 11:26:12

标签: android-studio httpclient

我在android studi上使用getInternetData函数中的HttpClient和defaultHttpClient方法。但是这些方法不再是SDK 23的一部分。我怎样才能克服这个问题。我也在build.gradle中使用了依赖项,但仍然存在错误。请告诉我还有其他方法可以打电话。?

公共类GetMethodEx {

public String getInternetData() throws Exception{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://api.twitter.com/1/statuses/public_timeline.json");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) !=null){
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        return data;
    }finally{
        if (in != null){
            try{
                in.close();
                return data;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}   

}

0 个答案:

没有答案