RequestParams之间的差异增加()和put()在AndroidAsyncHttp [英] Difference between RequestParams add() and put() in AndroidAsyncHttp

查看:216
本文介绍了RequestParams之间的差异增加()和put()在AndroidAsyncHttp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 Android的异步HTTP 库我偶然发现了 params.add()

我一直在使用 params.put()一段时间,似乎比最好加()因为它允许比的字符串的其他数据类型(如 INT 对象文件),而添加()没有。​​

RequestParams PARAMS =新RequestParams();//那么这是怎么
params.add(param_a,ABC);//与此不同
params.put(param_a,ABC);//我应该使用哪一个?


解决方案

的区别主要在两个(除添加() s字符串只支持)是把()覆盖previous $ p $ 参数与psence而现有键添加()没有。​​

例如:

params.put(等,等等);
params.put(钥匙,ABC);
params.put(钥匙,XYZ);// PARAMS:等=等和放大器;关键= XYZ

虽然附加创​​建两个 PARAMS 使用相同的密钥:

params.add(等,等等);
params.add(钥匙,ABC);
params.add(钥匙,ABC);// PARAMS:等=等和放大器;关键= ABC&放大器;关键= XYZ


但是,什么是这样做的重要性?

在上面的例子中,Web服务器将只读取的最后一个值 XYZ 而不是 ABC 但这有用发表 ING阵列

params.add(键[],A);
params.add(键[],B);
params.add(键[],C);// params:一个键[] = A和键[] = B和键[] = C
//服务器将它读成:钥匙=> [一,B,C]

While using the android-async-http library I stumbled upon params.add().

I've been using params.put() for a while and it seems better than add() since it allows data types other than String (like int, long, object, file) while add() does not.

RequestParams params = new RequestParams();

// So how is this
params.add("param_a", "abc");

// different from this
params.put("param_a", "abc");

// and which one should I use?

解决方案

The major difference between the two (other than add()'s String-only support) is that put() overwrites the previous presence of param with an existing key while add() does not.

For example:

params.put("etc", "etc");
params.put("key", "abc");
params.put("key", "xyz");

// Params: etc=etc&key=xyz

While add creates two params with the same key:

params.add("etc", "etc");
params.add("key", "abc");
params.add("key", "abc");

// Params: etc=etc&key=abc&key=xyz


But what is the importance of doing this?

In the above example, the web-server would only read the last value of key i.e. xyz and not abc but this is useful when POSTing arrays:

params.add("key[]", "a");
params.add("key[]", "b");
params.add("key[]", "c");

// Params: key[]=a&key[]=b&key[]=c
// The server will read it as: "key" => ["a", "b", "c"]

这篇关于RequestParams之间的差异增加()和put()在AndroidAsyncHttp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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