1

I searched for good REST / HTTP Clients for Android-Development, but I did not found something that meets my needs.

Now I have a question. Is there a good way to reuse HTTP client-objects in an android project?

It would be good if there is a central place (maybe static class?) to access an httpclient to do request (async)...

How can I do this in an android project?

Same thing for my credentials. (I use basic auth and do not want to pass the credentials from activity to activity)

2
  • are you REST services serving JSON content? Commented Mar 9, 2013 at 21:48
  • yes, I use JSON content. btw: sorry I did not meant the R class (I meant the ressources, but they are xml files... so... :P) Commented Mar 9, 2013 at 22:13

3 Answers 3

2

I would recommend you RoboSpice:

RoboSpice is a modular android library that makes writing asynchronous network requests easy.

It does have a cache manager, and works as service, which is, to me, better than common AsyncTasks.

https://github.com/octo-online/robospice

Also, you should not touch the R class, it is automatically built from your ressources.

Sign up to request clarification or add additional context in comments.

1 Comment

<Super extra bold with a million exclamations> "you should not touch the R class"</>
1

I suggest checking out

https://github.com/posco2k8/rest_loader_tutorial


I would also suggest a minor improvement change RESTLoader.RESTResponse data so as data be the final data in that case List<String> so as the parsing moved to the acync loader and avoid latency on UI thread. Adding the code pointing out the suggestions for change.

@Override
public void onLoadFinished(Loader<RESTLoader.RESTResponse> loader, RESTLoader.RESTResponse data) {
    int    code = data.getCode();
    String json = data.getData(); // <-- (change) this should be the final data

    // Check to see if we got an HTTP 200 code and have some data.
    if (code == 200 && !json.equals("")) {

        // For really complicated JSON decoding I usually do my heavy lifting
        // Gson and proper model classes, but for now let's keep it simple
        // and use a utility method that relies on some of the built in
        // JSON utilities on Android.
        List<String> tweets = getTweetsFromJson(json); // <-- (change) that parse move to loader

        // Load our list adapter with our Tweets.
        mAdapter.clear();
        for (String tweet : tweets) {
            mAdapter.add(tweet);
        }
    }
    else {
        Toast.makeText(this, "Failed to load Twitter data. Check your internet settings.", Toast.LENGTH_SHORT).show();
    }
}

Comments

0

As said RoboSpice is a very good choice. If you want to simply keep your local resource syncronized with remote data you can use RestDroid which is a "resource oriented" library to handle asynchronous REST request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.