0
I’m doing a JSON integration in my application, but when I use DefaultHttpClient
displays the message:
'org.apache.http.impl.client.Defaulthttpclient' is deprecated.
Follow the code below:
public class JsonClass {
InputStream input = null;
JSONObject jObect = null;
String json = "";
//Recebe sua url
public JSONObject getJSONFromUrl(String url) {
//HTTP request
try {
// default HttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
input = httpEntity.getContent();
} catch (UnsupportedEncodingException | ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
input, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
input.close();
json = sb.toString();
Log.i("JRF", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// Transforma a String de resposta em um JSonObject
try {
jObect = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// retorna o objeto
return jObect;
}
}
In some classes such as DefaultHttpClient
, HttpPost
, HttpResponse
and so on, they appear scratched in the code, and they present this message.
Yes it is correct what it says, then what is your question?
– ramaral
Actually I wanted to know why this occurs, if it results in the project, because it presents error when trying to run the project with the above class.
– Jonathan Sales
Google recommends using the class
HttpURLConnection
in place ofDefaultHttpClient
from android Gingerbread as you can read in this post. However nothing prevents you from continuing to use the classDefaultHttpClient
.– ramaral