I want to achieve a rest client on Android with my custom Grape API (Ruby on Rails) and was wondering where to start (in others words, if there’s good practice about implementing it on an Android OS… or any good and RECENT example about it.)
- So far, I’ve been checking out this video on Rest from a Google talk.
- I came across this code for a rest client too.
- And just started reading this.
Thank you. I’ll be posting my solution soon.
So basically, I have a method in my rest API (in Grape) that looks something like this.
“http://localhost:3000/api/signin/”
Basically, if I do this command line I can have access/authenticate:
curl -H "Accept: application/vnd.lzgo-v1+json" -d email=myuser -d password=mypassword http://localhost:3000/api/signin/
Now, I’m attempting to build a kind of generic way to access my API obviously on android.
So far, I’ve been going with this guy implement with the RestService.
Intent intent = new Intent(activity, com.android.lzgo.service.RESTService.class);
intent.setData(Uri.parse("http://localhost:3000/api/signin/"));
Bundle params = new Bundle();
params.putString("email", "myuser");
params.putString("password", "mypassword");
intent.putExtra(RESTService.EXTRA_HTTP_VERB, RESTService.POST);
intent.putExtra(RESTService.EXTRA_PARAMS, params);
intent.putExtra(RESTService.EXTRA_RESULT_RECEIVER, getResultReceiver());
Then I’m kind of confuse in the RestService implementation, when my case is a POST.
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramsToList(params));
postRequest.setHeader("Accept", "application/vnd.lzgo.v1+json");
postRequest.setEntity(formEntity);
If you look at my URL on top, it’s seems more like parameters, so I might use instead postRequest.setParams()…
I have learned, when you use the emulator, localhost refers to the device’s own loopback service, not the one on your machine as you may expect.
I had to use 10.0.2.2 to access your actual machine, it is an alias set up to help in development.