我正在尝试从android应用程序调用https post api。它给出403权限被拒绝错误。但是从Windows cmd中的邮递员或CURL调用时,该api可以正常工作。 在Android中,我尝试了Volley和OkHttpClient,但是两者的响应是相同的。我正在为两个库附加代码。任何帮助,将不胜感激。预先感谢。
排球
RequestQueue queue = Volley.newRequestQueue(getActivity());
String url = "https://xxxxx.com/api.php";
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response.
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String, String>();
MyData.put("content1", "xyz");
MyData.put("content2", "xyz");
return MyData;
}
}; queue.add(MyStringRequest);
OkHttpClient
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("content1", "xyz")
.add("content1", "xyz")
.build();
okhttp3.Request request = new okhttp3.Request.Builder()
.url(url)
.post(formBody)
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
// Do something with the response.
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
确保oyu的标题中具有正确的内容类型。例如在排球
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-type", "application/x-www-form-urlencoded");
return params;
}