Link to home
Start Free TrialLog in
Avatar of Chris Jones
Chris JonesFlag for United States of America

asked on

import org.apache.http.HttpEntity; to legacy for android


i have some android code that uses
import org.apache.http.HttpEntity;

i am trying to load the legacy code so my code will not have deprecated sections in it. i have updated the file and added the .jar files but the deprecated code still shows in my code.
Avatar of Chris Jones
Chris Jones
Flag of United States of America image


or is there a workaround to correct my function

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new  HttpPost("");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                        sb.append(line + "\n");
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                return result;

Open in new window


Hi Chris Jones,

useLibrary 'org.apache.http.legacy' in gradle. Download the jar file from here( and add into the project. Call webservice httpclient replace by httpurlconnection.


Try other alternative (, Here is what i am recommend you. I am using this in my projects.

Thanks & Regards,
Krunal T. Tailor
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial