如何在 android RequestParams() 中传递嵌套参数
How to pass nested params in android RequestParams()
我正在使用 android-async-http 库通过使用 RequestParams()
传递参数从 url 获取 json,当参数未嵌套时它可以在没有嵌套的情况下工作任何问题,但我的 url 包含嵌套参数,我不明白如何将这些参数添加到 RequestParams()
URL 从以下位置获取数据:
https://www.someurl.com/something/v3/something/something?view=READER&fields=description,locale(country,language),name,pages/totalItems,posts/totalItems,published,updated,url&key=my_key
我想知道如何添加 locale(country,language)
和 pages/totalItems
我的 MainActivity
public void getBlogInformation() throws JSONException {
RequestParams params = new RequestParams();
params.put("key", "123asdf456ghjklabcdefghijklmn");
params.put("view", "reader");
params.put("fields", "description");
//How to add next params???
params.put("locale", );
BlaBlaRESTClient.get("", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
Log.d("Response ", ""+response);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.d("Error: ", ""+errorResponse);
}
});
}
您的查询字符串具有以下字段值对(或参数):
- 查看: READER
- 字段:描述,语言环境(国家,语言),名称,pages/totalItems,posts/totalItems,已发布,已更新,url
- 键: my_key
没有'locale'这样的参数,它是你的一个值的一部分。所以你不能将它作为
params.put("locale", "[whateveryouputhere]");
因为它的输出类似于:
...?view=reader&locale=[whateveryouputhere]&...
我现在假设您正在调用的 API 不在您的控制之下,并且参数必须采用该特定形式。这意味着您必须 URL 对值进行编码,因为它包含的字符与 URLS:
中使用的字符冲突
description,locale(country,language),name,pages/totalItems,posts/totalItems,published,updated,url
作为
description%2Clocale(country%2Clanguage)%2Cname%2Cpages%2FtotalItems%2Cposts%2FtotalItems%2Cpublished%2Cupdated%2Curl
所以你会像这样填充你的 RequestParameters:
params.put("key", "123asdf456ghjklabcdefghijklmn");
params.put("view", "reader");
params.put("fields", "description%2Clocale(country%2Clanguage)%2Cname%2Cpages%2FtotalItems%2Cposts%2FtotalItems%2Cpublished%2Cupdated%2Curl");
当然,您会用适当的描述替换 'description',用整数替换 'totalItems'。但原理是一样的。然后服务器将获取参数 fields 并将字符串值解析为其单独的值。
详情请看这里:
URL Query String Wikipedia
请参阅此处了解 URL 编码:
URL Encoding/Decoding Tool
我正在使用 android-async-http 库通过使用 RequestParams()
传递参数从 url 获取 json,当参数未嵌套时它可以在没有嵌套的情况下工作任何问题,但我的 url 包含嵌套参数,我不明白如何将这些参数添加到 RequestParams()
URL 从以下位置获取数据:
https://www.someurl.com/something/v3/something/something?view=READER&fields=description,locale(country,language),name,pages/totalItems,posts/totalItems,published,updated,url&key=my_key
我想知道如何添加 locale(country,language)
和 pages/totalItems
我的 MainActivity
public void getBlogInformation() throws JSONException {
RequestParams params = new RequestParams();
params.put("key", "123asdf456ghjklabcdefghijklmn");
params.put("view", "reader");
params.put("fields", "description");
//How to add next params???
params.put("locale", );
BlaBlaRESTClient.get("", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
Log.d("Response ", ""+response);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.d("Error: ", ""+errorResponse);
}
});
}
您的查询字符串具有以下字段值对(或参数):
- 查看: READER
- 字段:描述,语言环境(国家,语言),名称,pages/totalItems,posts/totalItems,已发布,已更新,url
- 键: my_key
没有'locale'这样的参数,它是你的一个值的一部分。所以你不能将它作为
params.put("locale", "[whateveryouputhere]");
因为它的输出类似于:
...?view=reader&locale=[whateveryouputhere]&...
我现在假设您正在调用的 API 不在您的控制之下,并且参数必须采用该特定形式。这意味着您必须 URL 对值进行编码,因为它包含的字符与 URLS:
中使用的字符冲突description,locale(country,language),name,pages/totalItems,posts/totalItems,published,updated,url
作为
description%2Clocale(country%2Clanguage)%2Cname%2Cpages%2FtotalItems%2Cposts%2FtotalItems%2Cpublished%2Cupdated%2Curl
所以你会像这样填充你的 RequestParameters:
params.put("key", "123asdf456ghjklabcdefghijklmn");
params.put("view", "reader");
params.put("fields", "description%2Clocale(country%2Clanguage)%2Cname%2Cpages%2FtotalItems%2Cposts%2FtotalItems%2Cpublished%2Cupdated%2Curl");
当然,您会用适当的描述替换 'description',用整数替换 'totalItems'。但原理是一样的。然后服务器将获取参数 fields 并将字符串值解析为其单独的值。
详情请看这里: URL Query String Wikipedia
请参阅此处了解 URL 编码: URL Encoding/Decoding Tool