-1

I'm doing an AsyncTask that works I might add if I lower my sdk to 22.

on sdk 23 it doesn't work at all, and even in 22, it scribes some lines for me.

what am i doing wrong ? or how to correct it so that i could run it at sdk 23 ?

@Override
    protected Boolean doInBackground(String... urls) {

        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);

                //this gets us the actors node and puts it in jarray veriable
                JSONArray jarray = jsono.getJSONArray("actors");

                for (int i = 0; i < jarray.length(); i++) {
                    //the object veriable will be the node in position i inside the "actors" node in the main json
                    JSONObject object = jarray.getJSONObject(i);


                    Log.d("myAppLog", "next one is: ");
                    Log.d("myAppLog", object.getString("name"));

                    actorClass actorClassVeriable = new actorClass();


                    actorClassVeriable.setName(object.getString("name"));
                    actorClassVeriable.setDescription(object.getString("description"));
                    actorClassVeriable.setDob(object.getString("dob"));
                    actorClassVeriable.setCountry(object.getString("country"));
                    actorClassVeriable.setHeight(object.getString("height"));
                    actorClassVeriable.setSpouse(object.getString("spouse"));
                    actorClassVeriable.setChildren(object.getString("children"));
                    actorClassVeriable.setImage(object.getString("image"));

                    actorsList.add(actorClassVeriable);
                }
                return true;
            }

            //------------------>>

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        if(result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
}
  • it scribes out the > HttpGet, HttpClient ,HttpResponse and HttpEntity
Shay Vidas
  • 47
  • 10

2 Answers2

7

From documentation,

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

android {
    useLibrary 'org.apache.http.legacy'
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
3

HttpGet, HttpClient ,HttpResponse and HttpEntity are depreceated.
Now you have to use HttpUrlConnection

URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
    finally {
     urlConnection.disconnect();
   }
 }
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46