如何使用 Android 通过 Request 发送 JSON 对象?
我想将以下 JSON 文本发送
{"Email":"[email protected]","Password":"123456"}
到 Web 服务并读取响应。我知道如何读取 JSON。问题是上面的 JSON 对象必须以变量名 jason
的形式发送。
我怎样才能从安卓做到这一点?创建请求对象、设置内容标头等步骤是什么?
I want to send the following JSON text
{"Email":"[email protected]","Password":"123456"}
to a web service and read the response. I know to how to read JSON. The problem is that the above JSON object must be sent in a variable name jason
.
How can I do this from android? What are the steps such as creating request object, setting content headers, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用 Apache HTTP 客户端,从 Android 发送 json 对象很容易。这是有关如何执行此操作的代码示例。您应该为网络活动创建一个新线程,以免锁定 UI 线程。
您还可以使用 Google Gson 发送和检索 JSON。
Sending a json object from Android is easy if you use Apache HTTP Client. Here's a code sample on how to do it. You should create a new thread for network activities so as not to lock up the UI thread.
You could also use Google Gson to send and retrieve JSON.
Android 没有用于发送和接收 HTTP 的特殊代码,您可以使用标准 Java 代码。我建议使用 Android 附带的 Apache HTTP 客户端。这是我用来发送 HTTP POST 的代码片段。
我不明白在名为“jason”的变量中发送对象与什么有关。如果您不确定服务器到底想要什么,请考虑编写一个测试程序来将各种字符串发送到服务器,直到您知道它需要采用什么格式。
Android doesn't have special code for sending and receiving HTTP, you can use standard Java code. I'd recommend using the Apache HTTP client, which comes with Android. Here's a snippet of code I used to send an HTTP POST.
I don't understand what sending the object in a variable named "jason" has to do with anything. If you're not sure what exactly the server wants, consider writing a test program to send various strings to the server until you know what format it needs to be in.
Android Api Level 22 已弃用
HttpPost
。因此,请使用HttpUrlConnection
进行进一步操作。HttpPost
is deprecated by Android Api Level 22. So, UseHttpUrlConnection
for further.下面的链接提供了一个非常好的 Android HTTP 库:
http://loopj.com/android-async-http/< /a>
简单的请求非常简单:
发送 JSON(归功于 https://github 上的 `voidberg' .com/loopj/android-async-http/issues/125):
它都是异步的,与 Android 配合良好,并且可以安全地从 UI 线程调用。 responseHandler 将在您创建它的同一线程(通常是您的 UI 线程)上运行。它甚至有一个内置的 JSON resonseHandler,但我更喜欢使用 google gson。
There's a surprisingly nice library for Android HTTP available at the link below:
http://loopj.com/android-async-http/
Simple requests are very easy:
To send JSON (credit to `voidberg' at https://github.com/loopj/android-async-http/issues/125):
It's all asynchronous, works well with Android and safe to call from your UI thread. The responseHandler will run on the same thread you created it from (typically, your UI thread). It even has a built-in resonseHandler for JSON, but I prefer to use google gson.
现在,由于
HttpClient
已弃用,当前的工作代码是使用HttpUrlConnection
创建连接并写入连接和从连接读取。但我更喜欢使用 Volley。这个库来自android AOSP。我发现很容易使用JsonObjectRequest
或JsonArrayRequest
Now since the
HttpClient
is deprecated the current working code is to use theHttpUrlConnection
to create the connection and write the and read from the connection. But I preferred to use the Volley. This library is from android AOSP. I found very easy to use to makeJsonObjectRequest
orJsonArrayRequest
没有什么比这更简单的了。使用 OkHttpLibrary
创建您的 json
并像这样发送它。
Nothing could be simple than this. Use OkHttpLibrary
Create your json
and send it like this.