how to call RESTful web service from android?

有些话、适合烂在心里 提交于 2019-12-02 20:23:12

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6046409/how-to-call-restful-web-service-from-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!