无法在android中实例化类型httpclient [英] cannot instantiate the type httpclient in android

查看:213
本文介绍了无法在android中实例化类型httpclient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码行出现错误 HttpClient Client =新的HttpClient. 我尝试将其重写为HttpClient Client =新的DefaultHttpClient.但却解决了问题,并使用其他方法创建了新错误

Am getting an error with the following line of code HttpClient Client= new HttpClient. I have tried rewriting it as HttpClient Client= new DefaultHttpClient. but it solves the problem and creates a new error with other methods

. 这是我的代码一瞥

protected static MDSResult doExecute(Context ctx, HttpMethod method){
    HttpClient client = new HttpClient();
    SharedPreferences preferences = 
        PreferenceManager.getDefaultSharedPreferences(ctx);
    MDSResult response = null;
    // If there's a proxy enabled, use it.
    String proxyHost = preferences.getString(
            Constants.PREFERENCE_PROXY_HOST, "");
    String sProxyPort = preferences.getString(
            Constants.PREFERENCE_PROXY_PORT, "0");
    boolean useSecure = preferences.getBoolean(
            Constants.PREFERENCE_SECURE_TRANSMISSION, false);

    int proxyPort = 0;
    try {
        if (!"".equals(sProxyPort)) 
            proxyPort = Integer.parseInt(sProxyPort);
    } catch(NumberFormatException e) {
        Log.w(TAG, "Invalid proxy port: " + sProxyPort);
    }
    if (!"".equals(proxyHost) && proxyPort != 0) {
        Log.i(TAG, "Setting proxy to " + proxyHost + ":" + proxyPort);
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(proxyHost, (int)proxyPort);
        client.setHostConfiguration(hc);
    }
    // execute the Http/https method
    try {
        if(useSecure){
            ProtocolSocketFactory ssl = new SimpleSSLProtocolSocketFactory();
            Protocol https = new Protocol("https", ssl, 443);
            Protocol.registerProtocol("https", https);
        }
        int status = client.executeMethod(method); 
        Log.d(TAG, "postResponses got response code " +  status);

        char buf[] = new char[20560];
        Reader reader = new InputStreamReader(
                method.getResponseBodyAsStream());
        int total = reader.read(buf, 0, 20560);
        String responseString = new String(buf);
        Log.d(TAG, "Received from MDS:" + responseString.length()+" chars");
        Gson gson = new Gson();
        response = gson.fromJson(responseString, MDSResult.class);

    } 

推荐答案

您想要的是DefaultHttpClient,而不是您提到的HttpClient. HttpClient是抽象的,因此无法实例化.

You want DefaultHttpClient, not HttpClient like you mentioned. HttpClient is abstract so cannot be instantiated.

HttpClient client = new DefaultHttpClient();

使用该方法时,其他方法有什么错误?

What is the error with the other methods when you use that ?

为什么不使用 HttpURLConnection ?我发现它更可靠.

Also why not use HttpURLConnection ? I find it much more reliable.

这篇关于无法在android中实例化类型httpclient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆