Reputation: 929
I'm currently calling a local .json file in my Android app using the following line
InputStream inputStream = context.getAssets().open("cyclist.json");
I simply want to switch it to pull the .json from a webservice instead. What is the best way to do this?
Upvotes: 0
Views: 62
Reputation: 8911
Please, please, don't reinvent the wheel.
Use existing libraries Volley by Google (video from I/O talk), Retrofit by Square, RoboSpice and countless others are there to serve you. Further, search before posting
Upvotes: 1
Reputation: 4585
Supposing you already set up a server to respond to requests, I would try something like this:
URL url = new URL("http://www.mydomain.com/slug");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
readStream(in);
} finally {
in.close();
}
See URLConnection for details.
I know Android enforces limitations in downloading stuff from a server. You might have to execute the code in another thread, using the AsyncTask. Again, I'm not sure if this is required for your particular purpose.
Upvotes: 0