2

i'm using android studio API 23 and i have these warnings deprecated warning

Here's my code

@Override protected Boolean doInBackground(String... urls) { String strNama[], strDeskripsi[], strFoto[], strMarker[], strLng[], strLat[]; try { HttpGet httppost = new HttpGet(urls[0]); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); if (status == 200) { HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity); JSONObject jsono = new JSONObject(data); JSONArray konten = jsono.getJSONArray("konten"); strNama = new String[konten.length()]; strDeskripsi = new String[konten.length()]; strFoto = new String[konten.length()]; strMarker = new String[konten.length()]; strLat = new String[konten.length()]; strLng = new String[konten.length()]; for (int i = 0; i < konten.length(); i++) { JSONObject object = konten.getJSONObject(i); strNama[i] = object.getString("nama"); strDeskripsi[i] = object.getString("deskripsi"); strFoto[i] = object.getString("foto"); strMarker[i] = object.getString("marker"); strLat[i] = object.getString("lat"); strLng[i] = object.getString("lng"); Actors actor = new Actors(); actor.setName(strNama[i]); actor.setDescription(strDeskripsi[i]); actor.setImage(strFoto[i]); actor.setMarker(strMarker[i]); actor.setLat(strLat[i]); actor.setLng(strLng[i]); actorsList.add(actor); } return true; } } catch (ParseException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return false; } 

And here's in module

android { compileSdkVersion 23 buildToolsVersion "23.0.2" useLibrary 'org.apache.http.legacy' defaultConfig { applicationId "com.krb.navigasi.petakebunrayabogor" minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } productFlavors { } } 

It runs fine in android 5.0 but how can i solve these warnings? I hope there's someone can help me to fix my code above. Any help will be greatly appreciated.

Thanks in advance.

2
  • Apache Http module has been removed in the latest versions. So you have to choose another option like OkHttp, Volley something like this. Commented Mar 23, 2016 at 5:01
  • @Michael Julyus Christopher M.: decrease values for compileSdkVersion, buildToolsVersion and targetSdkVersion n see it shd work coz they are deprecated in API 23. Commented Mar 23, 2016 at 5:04

2 Answers 2

2

As it's been already answered that mentioned classes has been deprecated, Android documentations suggest you to use HttpURLConnection where you can handle network calls on your own make sure you write them off the main thread.

given bellow is an example how you can POST an entity using HttpURLConnection

 HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); // hear you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc. httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json` httpURLConnection.connect(); 

and when you are posting some data to the instance of the HttpURLConnection you can do it like this...

 JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("para_1", "arg_1"); jsonObject.addProperty("para_2", "arg_2"); DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); wr.writeBytes(jsonObject.toString()); wr.flush(); wr.close(); 

As this class comes in bundled with android framework no need add any library, but I would suggest you to use something like OkHTTP,

this will take care of network calls of other thread and given example shows how you can post

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); return response.body().string(); } 

This example is calling the network call on the main thread you rather want to make enqueue like this

 okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Can you help me to fix my code that i posted above? Sorry, i'm new in android :(
you may use the code in the answer, both snippet would work with minor changes, if you still face any issue pass me the URL from which you are getting the response that I may tell you what to do
0

Just initialize this,and sync

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.cli‌​ent:4.1.2' 

5 Comments

Have you run it in android 6.0? Is it works fine in android 6.0?
I have tried your edited code, but is shows me deprecated warning. But if i use your previous answer like this compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2' The deprecated warnings will disappear. So , Which one is true? Your previous answer or your edited answer?
Yeah, but i haven't try it in android 6.0. I have try it in android 5.0 and it works well. But I am still not sure to android 6.0, whether successful or not. Have you tried it directly on the android 6.0 device (not in emulator)?
Sorry, i can't try it because my device is android 5.0 and i can't try it using emulator because my laptop will lag if i run the emulator. Have you try it in android 6.0 ? Thanks anyway.
i said yes..it is working in 6.0...and you need to try it by yourself..and accept answer if it helped..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.