如何在Android Studio中使用多个参数调用API。

huangapple go评论70阅读模式
英文:

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 = &quot;https://blah,com&quot;;

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,url,null,
            new Response.Listener&lt;JSONObject&gt;() {


        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONArray jsonArray =response.getJSONArray(&quot;items&quot;);

                for (int i = 0; i&lt; jsonArray.length(); i++){
                    JSONObject article = jsonArray.getJSONObject(i);

                    String authorName = article.getString(&quot;article_author&quot;);
                    String imageUrl = article.getString(&quot;src&quot;);
                    String published = article.getString(&quot;first_published_at&quot;);
                    String description = article.getString(&quot;value&quot;);
                    String headline = article.getString(&quot;title&quot;);

答案1

得分: 1

尝试以下方法处理嵌套的JSON数组,
如何在Android Studio中使用多个参数调用API。

尝试以下方法获取结果,
如何在Android Studio中使用多个参数调用API。

英文:

Try this way to work with nested json array,
如何在Android Studio中使用多个参数调用API。

Try this to get the result,
如何在Android Studio中使用多个参数调用API。

答案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&lt;JSONObject&gt;() {

@Override
public void onResponse(JSONObject response) {
    textView.setText(&quot;Response: &quot; + response.toString());
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
    // TODO: Handle error

}
}){
   @Override
   protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
       Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
       params.put(&quot;parameter1 name&quot;,&quot;parameter1 Value&quot;);
       params.put(&quot;parameter2 name&quot;,&quot;parameter2 Value&quot;);
       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(&quot;view_as&quot;,&quot;subscriber&quot;);

答案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&lt;String, String&gt; params = new HashMap();
params.put(&quot;first_param&quot;, 1);
params.put(&quot;second_param&quot;, 2);

JSONObject parameters = new JSONObject(params);

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener&lt;JSONObject&gt;() { ... }

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(&quot;body&quot;);
Iterator jsonArrayIterator = jsonArray.iterator();
while(jsonArrayIterator.hasNext()) {
    JSONObject jsonObject = (JSONObject) jsonArrayIterator.next();
    JSONObject jsonValue = (JSONObject) jsonObject.get(&quot;value&quot;);
    JSONObject jsonOriginal = (JSONObject) jsonValue.get(&quot;original&quot;);
    JSONObject jsonWidth = (JSONObject) jsonOriginal.get(&quot;width&quot;);
}

huangapple
  • 本文由 发表于 2020年9月19日 14:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63965794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定