How to import classes like Httpclient, Defaulthttpclient etc

Asked

Viewed 6,010 times

4

I implemented these classes, but I can’t 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);
    }

}
  • I’m unable to import. Despite declaring the import, it doesn’t happen.

  • Caring isn’t enough. It’s in your classpath? Where did you declare the Apache library as a dependency? If not, see in the answer how you can do this.

  • I don’t know how to do this.

  • See in the answer below the part of build.gradle. Browse this file in your project and add the mentioned line where the dependencies are.

  • But if you use this dependency, I have to redo my code; you could edit it?

2 answers

6

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

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

The recommendation is to use either a "third-party" Client such as the OkHttp or the very UrlConnection.

Alas for the OkHttp:

Declaring dependence on your 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...
  • You could edit my code by putting this new implementation?

  • Do you really need it? It’s very simple to fit, in the case of UrlConnection just copy the new function and call it

  • Dude, I’m a beginner; I took this implementation to capture image with the smartphone camera and send it to the server. But I found it very complicated.

2


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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.