how to call RESTful web service from android? - android

I have written REST web service in netbean IDE using jersey framework and java. For every request user need to provide username and password , I know the authentication is not good.
Using curl command like :
curl -u username:password -X PUT http://localhsot:8080/user
Now I want to call REST web service from android class.What should I write? I am new to android. I have a android class which use DefaultHttpClient and CredentialUsernameAndPassword.
But when i run in eclipse, sometime I get runtime exception or sdk exception.
Do anyone give me sample code and suggestion?
thanks

Do you use basic authentication? if so: use this.
if (username != null && password != null) {
client.getCredentialsProvider().setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials(username, password));
}
or you can add those in a header as setHeader("Authentication", "Basic "+Base64EncodedString(username,pass);
What you are doing is using Proxy authentication. why?
this article may also be helpful somehow:
link
also the -u stands for the ntlm authorization so maybe look into that too. there were some topics where it said it is not supported on android or something but i am sure if you need it you can make a workaround.

Related

Account Manager + Cookie Based Authentication

Is it possible to use Android Account Manager using Cookie-based authentication? How (a code with a explanation would be much appreciated)?
I have seen many examples regarding authentication token, but that is not the case. I have just implemented cookie-based authentication on Python FLASK.
OBS.: I'm using Android Volley for the requests of the application.
All you need to do is to add this line in onCreate in your Application class:
CookieHandler.setDefault(new CookieManager());
this line will make your HttpUrlConnection hold cookies like browser, and since most of the http agents like Volley or okHttp are based on HttpUrlConnection they also will hold your cookies )

Grape Rails API with Android

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.

Android - Basic Authenticated HTTP Request

So basically i need my android app to connect to a web service using a url as such
"http://username:password#0.0.0.0" aka basic authentication.
obviously the username and password are checked by the web app before allowing access and otherwise doesn't allow the request.
my issue is that all the methods i try always say unauthorised (response code 401) regardless of what combination of classes and methods ive used to try and connect to the the url.
The web app in question is designed to return things only is un/pw clears otherwise it returns nothing, the web app and un/pw etc have all be checked and cleared.
so does anyone no the correct way to send a request to a url like that and have it work correctly?
android api8 btw
UPDATE
Turns out my issue is due to the web app using NTLM windows authentication which is not supported directly by androids/apache http library, investigating appropriate workarounds now
Here's some code form a really old project of mine. I used basic auth for some web service, and this worked at the time. I'm not sure if there are updated api's since then (this was Android 1.6), but it should still work.
HttpGet request = new HttpGet();
request.setURI(new URI(url));
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials(authUser, authPass);
BasicScheme scheme = new BasicScheme();
Header authorizationHeader = scheme.authenticate(credentials, request);
request.addHeader(authorizationHeader);
Basically, Basic HTTP auth is a simple hash of the user and password. The browser allows you to stuff these values in the url, but it actually does the work of adding the basic auth header to your request.

Send and retrieve Ruby on Rails query?

I don't know anything about Ruby, but I think what I'm trying to do is pretty simple. I have an app that needs to send a url query like this to a heroku database: http://dartmouth.heroku.com/dnd/search.json?query=sebastian, then receive the data that comes back and organize it for the user. How do I send and recieve a query like this?
EDIT: I downloaded Spring and added the rest template jar to my projects build path. I tried using this code:
String url = "http://dartmouth.heroku.com/dnd/" + dataBase + "json?query=" + searchContent;
RestTemplate rstTemplate = new RestTemplate();
PersonList pList = rstTemplate.getForObject(url, PersonList.class);
but "RestTemplate" is not recognized. Did I miss an installation step?
You need to start by making an HTTP request for the data and then parsing the results. I would suggest trying out the Spring Android library to accomplish this: http://www.springsource.org/spring-android
Check out the explanation here: http://mike.bailey.net.au/2011/02/json-with-ruby-and-rails/
You can use a plugin called HttpParty for sending the request. Ruby on rails will interpret the json response by using the json library. The example on the above mentioned page might make things clearer.

Post an image from Android client to Rails server without triggering InvalidAuthenticityToken

I'm building an Android app that runs off of a rails server. At first, when I tried to post simple String data to the server, I ran into an InvalidAuthenticityToken issue, but realized that I can bypass the authentication by setting the content type to "json"
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Constants.REST_HOST + "/add_comment");
post.addHeader("Content-Type", "application/json");
The next step was trying to get upload profile picture working. However, when I tried uploading a photo via a MultipartEntity post, setting the content type to "json" causes the following error
StandardError (Invalid JSON string):
but not setting the content type brings back the InvalidAuthenticityToken exception. What's the correct way to post an image to a rails server from a foreign Java client?
ActionController::InvalidAuthenticityToken
(ActionController::InvalidAuthenticityToken):
Based on Jesse's suggestion, I ended up using
protect_from_forgery :except => :upload_avatar_pic
to disable authenticity check, but only for a specific function, so checks for browser requests are still validated.
You can disable authenticity checking on API non-get calls from non-web clients. You can do this in a before filter
class ApiController < ApplicationController
skip_before_filter :verify_authenticity_token
def create
#or whatever
end
end
The problem solved by Jesse Wolgamott said, but the page show "You are being redirected" message when I submit the form (Update,create,show). In before that the page redirected correctly. I am using rails 2.3.8.how to resolve this?

Categories

Resources