【问题标题】:Calling an API with string parametrs in Android with Volley使用 Volley 在 Android 中调用带有字符串参数的 API
【发布时间】:2018-11-12 23:09:40
【问题描述】:

我正在尝试调用一个 API,每次用户创建帐户时将数据添加到 dynamoDB。 API 的 url 格式如下:

https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod? id=""&username=""&numero_passeport=""&decision=""

问题是当我使用 volley 调用 API 时出现此错误:

06-03 14:32:23.599 13503-14515/com.amazonaws.youruserpools.CognitoYourUserPoolsDemo E/Volley: [14796] BasicNetwork.performRequest: Unexpected response code 400 for https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod

我用来调用 API 的代码:

  StringRequest sr = new StringRequest(Request.Method.GET, "https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.e("HttpClient", "success! response: " + response.toString());
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("HttpClient", "error: " + error.toString());
                        }
                    })
            {
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("id","\"\"");
                    params.put("username","\"zaeae\"");
                    params.put("numero_passeport","\"\"");
                    params.put("decision","\"\"");

                    return params;
                }
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    return params;
                }
            };
            queue.add(sr);

【问题讨论】:

    标签: java android api android-volley


    【解决方案1】:

    ? id=""&

    这些是URL参数,不能通过getParams()传递

    您需要将它们附加到Request.Method.GET之后的字符串

    【讨论】:

    • 谢谢你的回答,你说的追加是什么意思?我尝试将它们添加到字符串 url 但我遇到了同样的问题
    • 如果这解决了您的问题,您可以使用帖子旁边的复选标记表示感谢
    【解决方案2】:

    试试这个

    StringRequest sr = new StringRequest(Request.Method.GET, "https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod?id="+Uri.encode(id)+"&username="+Uri.encode(username)+"&numero_passeport="+Uri.encode(numero_passeport)+"&decision="+Uri.encode(decision),
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.e("HttpClient", "success! response: " + response.toString());
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("HttpClient", "error: " + error.toString());
                        }
                    })
            {
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("id","\"\"");
                    params.put("username","\"zaeae\"");
                    params.put("numero_passeport","\"\"");
                    params.put("decision","\"\"");
    
                    return params;
                }
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    return params;
                }
            };
            queue.add(sr);
    

    同时传递id, username, numero_passeport and decision的值

    【讨论】:

    • 同样的问题,不同的错误:),我认为这是一个编码问题,因为参数必须是字符串格式:(“ID”)__ 06-03 15:46:07.976 21537-22495 /com.amazonaws.youruserpools.CognitoYourUserPoolsDemo E/Volley:[16015] BasicNetwork.performRequest:t3x9lg8utf.execute-api.us-east-2.amazonaws.com/… cours de traitement 的意外响应代码 502"
    猜你喜欢
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2014-04-11
    相关资源
    最近更新 更多