【问题标题】:cannot instantiate the type httpclient in android无法在 android 中实例化类型 httpclient
【发布时间】:2013-06-16 09:04:15
【问题描述】:

以下代码行出现错误 HttpClient 客户端 = 新的 HttpClient。 我尝试将其重写为 HttpClient Client= new DefaultHttpClient。但它解决了问题并使用其他方法创建了一个新错误

。 这是我的代码的一瞥

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);

    } 

【问题讨论】:

标签: httpclient


【解决方案1】:

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

HttpClient client = new DefaultHttpClient();

当你使用其他方法时有什么错误?

为什么不使用 HttpURLConnection 呢?我觉得它更可靠。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 2014-12-30
    • 2019-07-26
    相关资源
    最近更新 更多