user989557
user989557

Reputation: 929

calling REST websrevice via Android

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

Answers (2)

Delyan
Delyan

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

francoisr
francoisr

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

Related Questions