【问题标题】:android - package org.apache.http does not exist sdk 23 [duplicate]android - 包 org.apache.http 不存在 sdk 23 [重复]
【发布时间】:2015-08-30 11:21:42
【问题描述】:

我升级到最新的 sdk 版本 23。现在我的一些代码不再工作了。 这是我之前上过的用于获取 json 的课程:

public class Spots_tab1_json {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public Spots_tab1_json() {

}
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Cache-Control", "no-cache");
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
            }
            httpResponse = httpClient.execute(httpPost);
        } else if (method == GET) {
            if (params != null) {
                String paramString = URLEncodedUtils.format(params, "UTF-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);
            httpGet.addHeader("Cache-Control", "no-cache");
            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

}

}

有替代品吗?如果是这样,应该用什么代替?

谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    作为一种解决方法,将其添加到您的应用程序 build.gradle

    android {
        useLibrary 'org.apache.http.legacy'
    }
    

    https://developer.android.com/preview/behavior-changes.html#behavior-apache-http-client

    【讨论】:

    • 感谢您的回复,但我尝试将此代码添加到两个 gradles 但它不起作用。它给了我 Gradle DSL method not found: 'useLibrary()' error 。
    • *.com/questions/30856785/…看这里,我猜你需要更新依赖