android


Get JSON from URL - deprecated method?


I have been using this code to get JSON from a specified URL, but today I checked it, and everything in it is deprecated. It still works fine, but I want to know what is the new method of doing it?
Here's what I have:
#Override
protected String doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
StringBuilder url = new StringBuilder("some url");
HttpGet hg = new HttpGet(url.toString());
HttpResponse hr = client.execute(hg);
int status = hr.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity he = hr.getEntity();
String data = EntityUtils.toString(he);
jsonGet = new JSONObject(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
The deprecated objects are HttpClient, HttpGet, HttpResponse, HttpEntity and EntityUtils.
EDIT: As suggested in some questions, this way
HttpClient client = HttpClientBuilder.create().build();
does not work for me, as I am getting HttpClientBuilder cannot be resolved
Apache httpClient is deprecated with api level 22 you can read it about this blog.
there is a new client for android now and it is very good.
but you can use okhttp instead (also back compat is possible).
EDIT
check this link(URL.openConnection()). this blog was posted in 2011 but they mentioned the deprecation there first as far as i know.
First of all you have to search well on Google.
Here is your answer may it will helps you.
The HttpClient documentation points you in the right direction:
org.apache.http.client.HttpClient:
This interface was deprecated in API level 22.
Please use openConnection() instead. Please visit this webpage for further details.
means that you should switch to java.net.URL.openConnection().
Here's how you could do it:
URL url = new URL("http://some-server");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// read the response
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);
IOUtils documentation: Apache Commons IO
IOUtils Maven dependency: http://search.maven.org/#artifactdetails|org.apache.commons|commons-io|1.3.2|jar
Special Thanks to fateddy for this answer

Related Links

How to get mac address in Android native code
Android - Override getPackageName() in application and get component callback.
just want to ask that, can we add only one image in a gridView in anrroid
RemoteView global variable becomes null when the service class crashes
Video Selection from gallery code not working for Marshmallow, but working for Lollipop - Android
Udacity Sunshine project android Testdb error when assertTrue(locationId != -1)?
Inside the spinner how to set imageview at the right corner
How can I setup this Android xml layout in good format?
android in drag'n'drop ,img can be drag ,but not drop
Correct way to get the instance of Application in android
Recyclerview empty view appears
Image slider inside fragment child using ViewFlipper
Webviewclientcrome is not working in android below 5.0 versions
Showing values which is put in sharedpreferences are not showing in textView [duplicate]
android: bring current activity to front via notification
Storing user login details in android studio

Categories

HOME
gridview
codenvy
merge
c99
google-spreadsheet
formal-languages
swt
data-visualization
crystal-lang
rcloud
project
reactive-programming
ibeacon-android
redis-sentinel
supervisor
alpacajs
esoteric-languages
sar
asciidoctor-pdf
object-storage
short-url
dapper-extensions
surveymonkey
has-and-belongs-to-many
progress-bar
volatile
livelink
scatter3d
python-2.6
paragraph
nested-lists
indy10
sql-delete
yowsup
roblox
swagger-php
nohup
protein-database
odroid
bindingnavigator
xmldocument
show-hide
procfile
plan-9
xbmc
trojan
tinkerpop
colorbar
git-flow
ngmaterial
type-theory
java-websocket
d3-force-directed
structuremap4
visual-studio-2008-sp1
vulcanize
neon
i386
udpclient
pre-build-event
http-status-code-401
bizagi
gcloud-ruby
database-relations
spring-retry
xenomai
rewrite
dulwich
ansi
node-serialport
cryptographichashfunction
infosphere-spl
variadic-templates
at-job
packet-sniffers
administration
entry-point
universal-image-loader
rmysql
cuba
xml-dsig
grails-plugin-rabbitmq
ray
dsym
websphere-esb
conditionaltagsupport
openargs
ora-06550
ie8-compatibility-mode
code-duplication
ice-cube
oocss
cffile
cnf
process-explorer
superscrollorama
dopostback
tree-balancing
panda3d
midlet
objectquery
koken
luabind
convention
timertask
vs-android
office-2007
message-passing
tabpanel
visualj#
windows-identity

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile