【问题标题】:How to access a password protected URL in android using volley library如何使用 volley 库在 android 中访问受密码保护的 URL
【发布时间】:2020-07-18 11:37:20
【问题描述】:

我正在尝试执行以下任务:

我计划在我的代码中使用 JsonObjectRequest(Volley 库)并提取凭据,但我无法理解请求中哪里需要用户名和密码。这是一个代码sn-p。如果有人能告诉我需要在此代码 sn-p 中验证用户名和密码以获取 JSON 对象的位置,那将非常有帮助。

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
                VolleyLog.wtf(response.toString());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.wtf(error.getMessage(), "utf-8");
        }
    });

queue.add(jsonObjectRequest)

【问题讨论】:

    标签: android api android-volley


    【解决方案1】:

    这是在 volley 中使用 POST 方法的方法:

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(final String response) {
                    try {
                        JSONObject object = new JSONObject(response);
    //                    here is your json object
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
    
    
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    //                volley errors
                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("username", username);
                    params.put("password", password);
                    return params;
                }
            };
    
        queue.add(stringRequest);
    

    【讨论】:

    • 非常感谢。我目前有11个声望。如果您赞成我的问题,我将能够赞成您的回答。另一件事。问题有 Json 对象。是否可以使用 StringRequest 而不是 JsonObjectRequest 来检索它?
    • 我发现有时 JsomObjectRequest 不能正常工作。 StringRequest 可以在任何情况下使用。
    • 如果用户输入错误的用户名和密码会发生什么。错误将如何处理?
    • 记录字符串响应并查看提供错误密码和用户名时得到的 json。然后你可以在json输出上添加条件(if/else)。
    • 我发现您使用的 URL 通过输入错误的用户名和密码给出了 {"error": "INVALID USER"}。所以你可以简单地添加 if (object.has("error")){ String error = object.getString("error"); } else { // 你的代码在这里 }
    【解决方案2】:

    使用用户名和密码创建一个jsonObject,然后将此对象传递给JsonObjectRequest

    您的代码将是这样的:

            JSONObject body= new JSONObject();
    
                body.put("username", "user");
                body.put("password", "userPassword");
    
                JsonObjectRequest jsonObjectRequest= new JsonObjectRequest(Request.Method.POST, url, 
                  body, new Response.Listener<JSONObject>() {
                 @Override
            public void onResponse(JSONObject response) {
                    VolleyLog.wtf(response.toString());
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.wtf(error.getMessage(), "utf-8");
            }
        });
    
    queue.add(jsonObjectRequest)
    

    【讨论】:

      【解决方案3】:

      你需要了解what is form-data以及它是如何使用的:

      定义和用法

      method属性指定如何发送form-data(form-data发送到action属性中指定的页面)。

      表单数据可以作为 URL 变量(使用 method="get")或作为 HTTP post 事务(使用 method="post")发送。

      GET注意事项

      • 以名称/值对的形式将表单数据附加到 URL 中
      • URL 的长度是有限的(大约 3000 个字符)
      • 切勿使用 GET 发送敏感数据! (将在 URL 中可见)
      • 对于用户想要为结果添加书签的表单提交很有用
      • GET 更适合用于非安全数据,例如 Google 中的查询字符串

      POST注意事项

      • 在 HTTP 请求正文中附加表单数据(数据未显示在 URL 中)
      • 没有大小限制

      要将用户名和密码参数作为表单数据传递,您可以创建 StringRequest 并覆盖它的 getParams 方法以返回数据映射。

      StringRequest request = new StringRequest(
          Request.Method.POST,
          requestUrl,
          onResultListener,
          onErrorListener) {
              @Override
              protected Map<String, String> getParams() {
                  HashMap<String, String> hashMap = new HashMap<>();
                  hashMap.put("username", username)
                  hashMap.put("password", password)
                  return hashMap;
              }
      
              @Override
              protected Response<String> parseNetworkResponse(NetworkResponse response) {
                  // You can parse response here and throw exceptions when required.
                  return super.parseNetworkResponse(response);
              }
          };
      queue.add(request)        
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        • 1970-01-01
        • 2011-07-01
        • 2011-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多