How to import classes like HttpClient, DefaultHttpClient etc

4

I've implemented these classes, but I can not import them; the project was done in Android Studio.

public class HttpConnection {

    public static String getSetDataWeb(WrapData wd){
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(wd.getUrl());
        String answer = "";

        try{
            ArrayList<NameValuePair> valores = new ArrayList<NameValuePair>();
            valores.add(new BasicNameValuePair("method", wd.getMethod()));
            valores.add(new BasicNameValuePair("produto", wd.getProduto()));
            valores.add(new BasicNameValuePair("marca", wd.getMarca()));
            valores.add(new BasicNameValuePair("detalhe", wd.getDetalhe()));
            valores.add(new BasicNameValuePair("img-mime", wd.getImage().getMime()));
            valores.add(new BasicNameValuePair("img-image", wd.getImage().getBitmapBase64()));

            httpPost.setEntity(new UrlEncodedFormEntity(valores));
            HttpResponse resposta = httpClient.execute(httpPost);
            answer = EntityUtils.toString(resposta.getEntity());
        }
        catch (UnsupportedEncodingException e) { e.printStackTrace(); }
        catch (ClientProtocolException e) { e.printStackTrace(); }
        catch (IOException e) { e.printStackTrace(); }
        return(answer);
    }

}
    
asked by anonymous 20.10.2015 / 01:14

2 answers

1

My question was to be able to import the HttpClient class. Actually this class is already obsolete, but as I said, I wanted to learn to import it. After some research I discovered that I should add this to my gradle :

android {
    useLibrary 'org.apache.http.legacy'
}
    
20.10.2015 / 19:25
6

It is not recommended to use the HttpClient class anymore because of some issues related to performance (compression, cache, and reduction of battery consumption).

So much so that in version 6, HttpClient is obsolete, as can be seen here . It's still possible to use, but if it's a new application, it's good to not even use it.

The recommendation is to use either a third-party client such as OkHttp or UrlConnection itself.

Be it by OkHttp :

Declaring the dependency on its build.gradle :

compile 'com.squareup.okhttp:okhttp:2.5.0'

Using:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
        .url(URL)
        .build();

Response response = client.newCall(request).execute();

if (response.code() != 200) {
    // ERRO
} else {
    String body = response.body().string();
    // Usar o body retornado pelo servidor...
}

Or Using UrlConnection :

private static String getUrlContents(String theUrl) {
    StringBuilder content = new StringBuilder();

    try {
        URL url = new URL(theUrl);

        URLConnection urlConnection = url.openConnection();

        BufferedReader bufferedReader =
                new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

        String line;

        while ((line = bufferedReader.readLine()) != null) {
            content.append(line + "\n");
        }

        bufferedReader.close();
    } catch(Exception e) {
        // Eh bom tratar as exceptions :D
        e.printStackTrace();
    }

    return content.toString();
}

String body = getUrlContents(URL);
// Usar o body retornado pelo servidor...
    
20.10.2015 / 01:56