java Android 休息客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13403507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:35:52  来源:igfitidea点击:

Android Rest Client

javaandroidapache-httpclient-4.x

提问by user470169

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 的示例,但所有示例都令人困惑,请有人向我解释一种使用 http 请求的方法。

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

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

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

但是响应始终为空,并且通过发布相同的数据使用浏览器工具进行测试时这些工作正常。我的方法有什么问题吗?请给我建议正确的方法。谢谢

回答by Sree Rama

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

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

(2) search in android developer blog

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

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

(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)

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

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

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

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

有一个名为 Volley 的新库,它看起来比 AsyncTask 更好。它在开发 RESTful 客户端时应该很有用。

回答by dipu

You probably forgot to add the internet permission to the manifest file. Add the following line.

您可能忘记向清单文件添加 Internet 权限。添加以下行。

 <uses-permission android:name="android.permission.INTERNET" />

回答by Bhavik Ambani

I thing you should try this,

我觉得你应该试试这个,

HttpContext localContext = new BasicHttpContext();
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,localContext);

Hope this will help.

希望这会有所帮助。

回答by Damodar Periwal

By any chance, is the server expecting a GET request for this operation? If so, you may want to use HttpGet instead of HttpPost.

无论如何,服务器是否期待此操作的 GET 请求?如果是这样,您可能希望使用 HttpGet 而不是 HttpPost。