我试图弄清楚如何通过使用HTTPClient从Android发布JSON。我试图解决这一问题已有一段时间,我在网上找到了很多示例,但我无法使用其中的任何示例。我认为这是因为我总体上缺乏JSON
/网络知识。我知道那里有很多示例,但是有人可以指出我的实际教程吗?我正在寻找包含代码的逐步过程,并解释为什么您执行每个步骤或该步骤执行的操作。不需要很复杂,简单就足够了。
再说一次,我知道那里有很多例子,我只是在寻找一个例子,解释一下到底发生了什么以及为什么这样做。
如果有人知道一本关于Android的好书,那么请告诉我。
再次感谢@terrance的帮助,这是我在下面描述的代码
public void shNameVerParams() throws Exception{
String path = //removed
HashMap params = new HashMap();
params.put(new String("Name"), "Value");
params.put(new String("Name"), "Value");
try {
HttpClient.SendHttpPost(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在这个答案中,我使用Justin Grammens发布的示例。
JSON代表JavaScript对象符号。在JavaScript中,可以像这样object1.name
和这样
引用属性object['name'];
。本文中的示例使用了这部分JSON。
零件
A风扇对象,电子邮件为键,foo@bar.com为值
{
fan:
{
email : 'foo@bar.com'
}
}
因此,等效对象为fan.email;
或fan['email'];
。两者的值相同'foo@bar.com'
。
以下是我们的作者用来发出HttpClient
Request的内容。我并不声称自己是专家,因此,如果有人有更好的措词来表达某些术语,您就可以放心了。
public static HttpResponse makeRequest(String path, Map params) throws Exception
{
//instantiates httpclient to make request
DefaultHttpClient httpclient = new DefaultHttpClient();
//url with the post data
HttpPost httpost = new HttpPost(path);
//convert parameters into JSON object
JSONObject holder = getJsonObjectFromMap(params);
//passes the results to a string builder/entity
StringEntity se = new StringEntity(holder.toString());
//sets the post request as the resulting string
httpost.setEntity(se);
//sets a request header so the page receving the request
//will know what to do with it
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
//Handles what is returned from the page
ResponseHandler responseHandler = new BasicResponseHandler();
return httpclient.execute(httpost, responseHandler);
}
如果您不熟悉Map
数据结构,请查看Java
Map参考。简而言之,地图类似于字典或哈希。
private static JSONObject getJsonObjectFromMap(Map params) throws JSONException {
//all the passed parameters from the post request
//iterator used to loop through all the parameters
//passed in the post request
Iterator iter = params.entrySet().iterator();
//Stores JSON
JSONObject holder = new JSONObject();
//using the earlier example your first entry would get email
//and the inner while would get the value which would be 'foo@bar.com'
//{ fan: { email : 'foo@bar.com' } }
//While there is another entry
while (iter.hasNext())
{
//gets an entry in the params
Map.Entry pairs = (Map.Entry)iter.next();
//creates a key for Map
String key = (String)pairs.getKey();
//Create a new map
Map m = (Map)pairs.getValue();
//object for storing Json
JSONObject data = new JSONObject();
//gets the value
Iterator iter2 = m.entrySet().iterator();
while (iter2.hasNext())
{
Map.Entry pairs2 = (Map.Entry)iter2.next();
data.put((String)pairs2.getKey(), (String)pairs2.getValue());
}
//puts email and 'foo@bar.com' together in map
holder.put(key, data);
}
return holder;
}
__
请随意评论有关此帖子的任何问题,或者如果我没有说清楚什么,或者如果我没有谈过您仍然感到困惑的事情,等等。
(如果贾斯汀·格莱门斯(Justin Grammens)不赞成,我将表示反对。但是如果没有,我要感谢贾斯汀对此的冷静。
我刚巧对如何使用代码发表评论,并意识到返回类型有误。方法签名设置为返回字符串,但在这种情况下,它未返回任何内容。我将签名更改为HttpResponse,并将引导您访问HttpResponse
的获取响应正文上的此链接,路径变量为url,并且我进行了更新以修复代码中的错误。
有人知道使用发布JSON的正确方法吗? 我从服务器得到响应。它使用Chrome工作。
问题内容: 我正在使用Retrofit集成我的Web服务,但我不明白如何使用POST请求将JSON对象发送到服务器。我目前陷入困境,这是我的代码: 活动:- PostInterface:- 要求JSON:- 响应JSON:- 问题答案: 在gradle中使用这些 使用这两个POJO类........ LoginData.class LoginResult.class 像这样使用API 使用这样的呼
我已经写了下面的代码发送头,张贴参数。问题是我使用的是SendAsync,因为我的请求可以是GET或POST。我如何将POST Body添加到这个peice代码中,这样,如果有任何POST Body数据,它就会被添加到我发出的请求中,如果它是简单的GET或POST而没有Body,它就会以这种方式发送请求。请更新以下代码:
我得到以下错误 响应数据为空。我做错了什么,或者我在代码中遗漏了什么?
我试图在ajax调用中发布json请求,但我没有收到来自请求的任何成功响应。 请找到我下面的代码:我在这里做错了什么: 它击中了网址,我得到200确定状态,但它总是出错条件... 有人能帮忙吗,我需要改变什么工作: 我尝试了data:JSON. stringify({key:"value", key1:"value e1"})-但这也没有帮助
问题内容: 有人知道使用JSON 的正确方法吗? 我从服务器收到响应。它可以使用Chrome浏览器。 问题答案: 对于 Guzzle <= 4: 这是原始的发布请求,因此将JSON放入正文即可解决问题