如何使用 volley 库访问 android 中受密码保护的 URL
How to access a password protected URL in android using volley library
我正在尝试执行以下任务:
我计划在我的代码中使用 JsonObjectRequest(Volley 库)并提取凭据,但我无法理解请求中哪里需要用户名和密码。这是一个代码片段。如果有人能告诉我需要在此代码片段中验证用户名和密码的位置以获取 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)
使用用户名和密码创建一个 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)
这就是你如何使用 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);
您需要了解what is form-data及其用法:
Definition and Usage
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
Notes on GET:
- Appends form-data into the URL in name/value pairs
- The length of a URL is limited (about 3000 characters)
- Never use GET to send sensitive data! (will be visible in the URL)
- Useful for form submissions where a user wants to bookmark the result
- GET is better for non-secure data, like query strings in Google
Notes on POST:
- Appends form-data inside the body of the HTTP request (data is not shown in URL)
- Has no size limitations
要将用户名和密码参数作为表单数据传递,您可以创建 StringRequest
并将其 getParams
方法覆盖到 return 数据映射。
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)
我正在尝试执行以下任务:
我计划在我的代码中使用 JsonObjectRequest(Volley 库)并提取凭据,但我无法理解请求中哪里需要用户名和密码。这是一个代码片段。如果有人能告诉我需要在此代码片段中验证用户名和密码的位置以获取 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)
使用用户名和密码创建一个 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)
这就是你如何使用 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);
您需要了解what is form-data及其用法:
Definition and Usage
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
Notes on GET:
- Appends form-data into the URL in name/value pairs
- The length of a URL is limited (about 3000 characters)
- Never use GET to send sensitive data! (will be visible in the URL)
- Useful for form submissions where a user wants to bookmark the result
- GET is better for non-secure data, like query strings in Google
Notes on POST:
- Appends form-data inside the body of the HTTP request (data is not shown in URL)
- Has no size limitations
要将用户名和密码参数作为表单数据传递,您可以创建 StringRequest
并将其 getParams
方法覆盖到 return 数据映射。
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)