Android 休息客户端 [英] Android Rest Client

查看:40
本文介绍了Android 休息客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多用于请求 REST API 的示例,但所有示例都令人困惑,有人可以向我解释一种使用 http 请求的方法吗.

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.

我的要求是,我想通过提供用户名、密码和密钥从 REST API 获取数据.

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

我使用的是,

        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

推荐答案

(1) Google I用于开发 REST 客户端的/O 视频会话

(2) 在android开发者博客中搜索

(2) search in android developer blog

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

请在发布您的问题后尝试,我可以共享基于 (1) & 开发的其余客户端的代码片段(2)

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

不要使用云到设备消息传递,而是使用最新的云方法进行 Android 应用程序开发.

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

有一个名为 Volley 的新库,它看起来比 AsyncTask 更好.它应该有助于开发 RESTful 客户端.

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

这篇关于Android 休息客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆