Forrest M
Forrest M

Reputation: 113

Cannot Resolve DefaultHttpClient in Android Studio

I am following this tutorial:https://www.youtube.com/watch?v=I58TkCi73ew

In about the middle of the video, Derek Banas inserts DefaultHttpClient to apparently allow browser/web access from the app. But when I put the exact code in the IDE, there is an error reporting that Android studio cannot resolve DefaultHttpClient. Here is the code with DefaultHttpClient:

class SaveTheFeed extends AsyncTask<Void, Void, Void>{

    String jsonString = "";

    String result = "";

    @Override
    protected Void doInBackground(Void... voids) {

        EditText translateEditText = (EditText) findViewById(R.id.editText);

        String wordsToTranslate = translateEditText.getText().toString();

        wordsToTranslate = wordsToTranslate.replace(" ", "+");

        DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());

        return null;
    }
}

And I tried to import these packages, but there was an error in the package as well.

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

I tried to look at other solutions in other questions, but none really appealed to my problem. Can anyone help me with this?

Thanks!☺

Note: this question is not a duplicate of Can't import org.apache.http.HttpResponse in Android Studio. First of all, I am not using Eclipse. Second of all, I tried the first solution from that question, but (it might have been my misunderstanding) OkHttp hasn't worked for me. It may also because my code format and style is different from the different packages' requirements.

Upvotes: 1

Views: 6508

Answers (2)

Shahbaz Hashmi
Shahbaz Hashmi

Reputation: 2815

Add this to build.gradle and synchronise.

android {

 useLibrary 'org.apache.http.legacy'

     }

By the way httpclient is deprecated now. You should go with @Clive reference.

dependencies {

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

}

Or add this in dependencies. httpclient is not deprecated in this.

Upvotes: 7

SGX
SGX

Reputation: 3353

Use HttpUrlConnection instead or use a library to do this, OkHttp library is a good one. http://square.github.io/okhttp/

Upvotes: 0

Related Questions