2

以下代码行 HttpClient Client= new 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);

    } 
4

1 回答 1

1

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

HttpClient client = new DefaultHttpClient();

当您使用它时,其他方法有什么错误?

另外为什么不使用HttpURLConnection?我觉得可靠多了。

于 2013-07-12T04:20:54.390 回答