0

This is the code that doesnt work. I found it online and i try to use it myself but the code of HTTP is always with an strok trough it and it doesnt work. i think its because its out dated but i have searched all over the internet and couldnt found an answer so i thought i might ask it here.

private class BackTask extends AsyncTask<Void,Void,Void> {
        ArrayList<String> list;
        protected void onPreExecute(){
            super.onPreExecute();
            list=new ArrayList<>();
        }
        protected Void doInBackground(Void...params){
            InputStream is=null;
            String result="";
            try{
                HttpClient httpclient=new DefaultHttpClient();
                HttpPost httppost= new HttpPost("http://10.0.2.2:8080/getInterviewees.php");
                HttpResponse response=httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                // Get our response as a String.
                is = entity.getContent();
            }catch(IOException e){
                e.printStackTrace();
            }

            //convert response to string
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    result+=line;
                }
                is.close();
                //result=sb.toString();
            }catch(Exception e){
                e.printStackTrace();
            }
            // parse json data
            try{
                JSONArray jArray =new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    JSONObject jsonObject=jArray.getJSONObject(i);
                    // add interviewee name to arraylist
                    list.add(jsonObject.getString("iname"));
                }
            }
            catch(JSONException e){
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void result){
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }
  • https://stackoverflow.com/questions/32949626/org-apache-http-entity-fileentity-is-deprecated-in-android-6-marshmallow – CommonsWare Feb 05 '17 at 14:14

3 Answers3

0
android {
compileSdkVersion ...
buildToolsVersion ...
useLibrary 'org.apache.http.legacy'

defaultConfig {
    ...
}

Use this on your build.gradle for app.

Pradeep Kumar Kushwaha
  • 2,231
  • 3
  • 23
  • 34
0

Try adding this in your gradle file.

android {
    useLibrary 'org.apache.http.legacy'
}
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
0

Or you can simply use httpUrlConnection instead of that...

sohaib karim
  • 281
  • 2
  • 12