英文:
How do i call API with multiple parameters in android studio
问题
我已经创建了一个应用程序,从API中提取数据并在列表中显示。我遇到的问题是,我无法从带有嵌套JSON数组的API中提取JSON数据。
在这张图片中,情况很简单,因为所有的信息都在一个数组/表中。
但是在这张图片中,对我来说就更困难了。例如,我如何调用段落值:body中的line呢?
这是我目前用于从API提取数据的代码。
private void parseJSON() {
String url = "https://blah,com";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject article = jsonArray.getJSONObject(i);
String authorName = article.getString("article_author");
String imageUrl = article.getString("src");
String published = article.getString("first_published_at");
String description = article.getString("value");
String headline = article.getString("title");
英文:
I have created this app that pulls data from an API and shows it in a list. the problem I am having is that I can't pull the JSON data from an API with a nested JSON array.
In this image it is simple since all the info is in one array / table.
https://i.stack.imgur.com/4UsBa.jpg
but in this image, it is more difficult for me. for example, how do i call the paragraph value: line in body?
https://i.stack.imgur.com/hqSKV.jpg
This is the code that i am currently using to pull data from API.
private void parseJSON () {
String url = "https://blah,com";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray =response.getJSONArray("items");
for (int i = 0; i< jsonArray.length(); i++){
JSONObject article = jsonArray.getJSONObject(i);
String authorName = article.getString("article_author");
String imageUrl = article.getString("src");
String published = article.getString("first_published_at");
String description = article.getString("value");
String headline = article.getString("title");
答案1
得分: 1
尝试以下方法处理嵌套的JSON数组,
尝试以下方法获取结果,
英文:
Try this way to work with nested json array,
Try this to get the result,
答案2
得分: 0
你的问题并不太清楚,但根据我理解,我认为你需要以下解决方案:
解决方案:在请求中发送参数
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
textView.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: 处理错误
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("parameter1 name","parameter1 Value");
params.put("parameter2 name","parameter2 Value");
return params;
};
在上面的示例中,我展示了一个 GET 请求的示例;假设 URL 是:"https://www.youtube.com/channel/UCXEIUll8VvOqRN-OSZ5_aOg",参数是 "view_as",参数值是 "subscriber",那么 params.put("view_as","subscriber");
。
英文:
Your question is not quiet clear, but what from what I understand I do think you need this solutions:
Solution: Sending Parameters along with the Request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
textView.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("parameter1 name","parameter1 Value");
params.put("parameter2 name","parameter2 Value");
return params;
};
As in the above example I have shown a get request; So suppose the URL is: "https://www.youtube.com/channel/UCXEIUll8VvOqRN-OSZ5_aOg" and parameter is "view_as" and parameter value is "subscriber" then params.put("view_as","subscriber");
答案3
得分: 0
解决方案: 如果您想将多个参数传递给您的请求,可以按照以下方法操作:
Map<String, String> params = new HashMap();
params.put("first_param", 1);
params.put("second_param", 2);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() { ... }
解决方案: 如果您想提取"items"数组并获取其后续对象内容,可以尝试使用迭代器而不是for循环的方法:
JSONArray jsonArray = (JSONArray) apiResult[0].get("body");
Iterator jsonArrayIterator = jsonArray.iterator();
while(jsonArrayIterator.hasNext()) {
JSONObject jsonObject = (JSONObject) jsonArrayIterator.next();
JSONObject jsonValue = (JSONObject) jsonObject.get("value");
JSONObject jsonOriginal = (JSONObject) jsonValue.get("original");
JSONObject jsonWidth = (JSONObject) jsonOriginal.get("width");
}
英文:
Solution: If you want to pass multiple parameters to your request then,
Map<String, String> params = new HashMap();
params.put("first_param", 1);
params.put("second_param", 2);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() { ... }
Solution: If you want to pull the items array and get its subsequent object contents, try using an iterator instead of a for loop,
JSONArray jsonArray = (JSONArray) apiResult[0].get("body");
Iterator jsonArrayIterator = jsonArray.iterator();
while(jsonArrayIterator.hasNext()) {
JSONObject jsonObject = (JSONObject) jsonArrayIterator.next();
JSONObject jsonValue = (JSONObject) jsonObject.get("value");
JSONObject jsonOriginal = (JSONObject) jsonValue.get("original");
JSONObject jsonWidth = (JSONObject) jsonOriginal.get("width");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论