Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - package org.apache.http does not exist sdk 23 [duplicate]

Tags:

android

I upgrade to latest sdk version 23. Now some codes of mine are not working anymore . this is a class I had before for getting 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;

}

}

is there any replacement ? if so , What should it be replace with ?

thanks

like image 771
navidjons Avatar asked Aug 30 '15 11:08

navidjons


1 Answers

as a workaround, add this to your application build.gradle

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

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

like image 140
Derek Fung Avatar answered Nov 13 '22 11:11

Derek Fung