Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java - Android Rest Client

I found so many samples for requesting a REST API, but all together are confusing, can some one please explain me a way to use http requests.

My Requirement is, I want to get data from a REST API by providing username, pwd and a key.

What I have Used was,

        HttpClient client = new DefaultHttpClient();  
        HttpPost post = new HttpPost("REST API url"); 
        post.setHeader("Content-type", "application/json");

        JSONObject obj = new JSONObject();
        obj.put("username", "un");
        obj.put("pwd", "password");
        obj.put("key","123456");

        post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
        HttpResponse response = client.execute(post);

But the response is always null and these working fine when tested with browser tool by posting the same data.Is some thing wrong with my approach? please suggest me the correct way. Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(1) Google I/O video session for developing REST clients

(2) search in android developer blog

(3) https://github.com/darko1002001/android-rest-client

Please try after that post your question, I can share code snippet from my rest client developed based on (1) & (2)

Do not use Cloud to Device Messaging, instead use the latest cloud approach with android application development.

There is new library called Volley, which looks better than AsyncTask. It should be useful in developing RESTful clients.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...