本文介绍了如何与And​​roid发送一个JSON对象了请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送以下 JSON 文本 {电子邮件:aaa@tbbb.com,密码:123456 } 到Web服务,并读取响应。我知道如何读 JSON 。现在的问题是,上述 JSON 对象必须在变量名发送杰森

I want to send the following JSON text {"Email":"aaa@tbbb.com","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.

我怎样才能做到这一点从Android的?需要哪些步骤,如创建请求对象,设置内容头部等。

How can I do this from android? What are the steps such as creating request object, setting content headers, etc.

推荐答案

Android不具备特殊的code发送和接收HTTP,您可以使用标准的Java code。我建议使用Apache HTTP客户端,它配备了Android系统。下面是code片断我用来发送一个HTTP POST。

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.

int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);

HttpPost request = new HttpPost(serverUrl);
request.setEntity(new ByteArrayEntity(
    postMessage.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);

这篇关于如何与And​​roid发送一个JSON对象了请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:42
查看更多