android httpClient在2.1中给出了语法错误,但在2.2中没有

时间:2012-05-17 18:30:11

标签: android httpclient

我的应用程序是2.2,但我希望它在2.1中。我做了目标更改,我在这里收到错误:

import android.net.http.AndroidHttpClient;

因为我读过它仅在2.2及以上版本中受支持。所以我做了以下替换:

import org.apache.http.client.HttpClient;

但是我在这里遇到语法错误:

httpclient.close(); ## error states add cast

整个代码在这里:

public static HttpClient httpclient = null;
try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
            bis.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpclient != null) {
                httpclient.close();
            }
        }

2 个答案:

答案 0 :(得分:0)

可能是因为Httpclient没有方法close(), 但是我建议你使用DefaultHttpClient,这里有一个提示:

DefaultHttpClient mHttpClient = null;

每当你想关闭它时,只需这样称呼它:

mHttpClient.getConnectionManager().shutdown();

答案 1 :(得分:0)

HttpClient是一个未定义close()方法的界面:http://developer.android.com/reference/org/apache/http/client/HttpClient.html