在我的android应用程序中,我发送了一个截击POST请求,但它不起作用。相反,它发送的是空参数。
如果我输入url(https://blogurl.com/wp-json/wp/v2/comments?post=20081
Postman为JAVA OKHttp生成的代码如下:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://blogurl.com/wp-json/wp/v2/comments?post=20081&author_name=Ozuf&author_email=myemail@my.com&content=This_is_a_sampe_comment")
.post(null)
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "23f2c2587-e9eb-5446-3d73-a1a1a6814683")
.build();
Response response = client.newCall(request).execute();
这是我用来发送POST请求的代码:
public void submitComment() {
final String comment = commentContent.getText().toString().trim();
final String name = commentName.getText().toString().trim();
final String email = commentEmail.getText().toString().trim();
Log.d(TAG, "submitComment called");
if (allFieldsAreValid()) {
Log.d(TAG, "All fields are valid");
final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?";
final PostingComment postingComment = PostingComment.newInstance();
postingComment.show(getFragmentManager(), "fragmentDialog");
JsonObjectRequest postDetails = new JsonObjectRequest(Method.POST, postComment, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
parseResponse(response);
postingComment.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse for getPost called");
VolleyLog.d(TAG, "Error: " + error.getMessage());
postingComment.dismiss();
if (sthWrongAlert != null) {
sthWrongAlert.show();
}
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("post", comtUrl);
params.put("author_name", name);
params.put("author_email", email);
params.put("content", comment);
return params;
}
};
int retrytimes = 10;
RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postDetails.setRetryPolicy(policy);
//Creating requestqueue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request queue
requestQueue.add(postDetails);
Log.d(TAG, "Data sent is " + comment + name + email);
} else {
Log.d(TAG, "All fields are not valid");
}
}
就像我之前说的,请求是真的,但是参数没有随请求一起发送。
public void submitComment() {
String comment = null;
try {
comment = URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String name = null;
try {
name = URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String email = null;
try {
email = URLEncoder.encode(commentEmail.getText().toString().trim(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.d(TAG, "submitComment called");
if (allFieldsAreValid()) {
Log.d(TAG, "All fields are valid");
final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?post="+comtUrl+"&content="+comment+"&author_name="+name+"&author_email="+email;
final PostingComment postingComment = PostingComment.newInstance();
postingComment.show(getFragmentManager(), "fragmentDialog");
JsonObjectRequest postDetails = new JsonObjectRequest(Method.POST, postComment, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
parseResponse(response);
postingComment.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse for getPost called");
VolleyLog.d(TAG, "Error: " + error.getMessage());
postingComment.dismiss();
if (sthWrongAlert != null) {
sthWrongAlert.show();
}
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
};
int retrytimes = 10;
RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postDetails.setRetryPolicy(policy);
//Creating requestqueue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request queue
requestQueue.add(postDetails);
Log.d(TAG, "Data sent is " + comment + name + email);
} else {
Log.d(TAG, "All fields are not valid");
}
}
应用他的解决方案后,我的代码就是这样的。但我经常会遇到错误409。
06-20 19:13:26.405 25586-27084/com.jozuf.blog E/Volley: [6168] BasicNetwork.performRequest: Unexpected response code 409 for https://blog.url/wp-json/wp/v2/comments?post=20081content=Yyggggbbhhgggggg&author_nameRrrauthor_emailgf%40ff.com
您可以将参数作为JSONObject发送,如下所示:
JSONObject params = new JSONObject("{\"post\":\"" + comtUrl + "\"," +
...
+ "}"
你将不再需要getBodyContentType和getParams
不为JsonRequests调用getParams()。
你能做的只是附加到
后置命令额外的参数。例如:
final String comment = URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
final String name = URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
final String email = URLEncoder.encode(commentEmail.getText().toString().trim(), "UTF-8");
final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?comment="+comment+"&name="+name+"&email="+email;
在chat中调试后,发现的问题是这是一个POST
请求,但参数仍在请求url中发送。
在您的情况下,发送POST
响应将使参数以多部分形式出现在正文中,这需要修复。用下面的代码更新您的代码
public void submitComment() {
String comment = null;
try {
comment = URLEncoder.encode(commentContent.getText().toString().trim(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String name = null;
try {
name = URLEncoder.encode(commentName.getText().toString().trim(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String email = null;
try {
email = URLEncoder.encode(commentEmail.getText().toString().trim(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.d(TAG, "submitComment called");
if (allFieldsAreValid()) {
Log.d(TAG, "All fields are valid");
final String postComment = "https://blogurl.com/wp-json/wp/v2/comments?post="+comtUrl+"&content="+comment+"&author_name="+name+"&author_email="+email;
final PostingComment postingComment = PostingComment.newInstance();
postingComment.show(getFragmentManager(), "fragmentDialog");
JsonObjectRequest postDetails = new JsonObjectRequest(Method.POST, postComment, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
parseResponse(response);
postingComment.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse for getPost called");
VolleyLog.d(TAG, "Error: " + error.getMessage());
postingComment.dismiss();
if (sthWrongAlert != null) {
sthWrongAlert.show();
}
}
});
int retrytimes = 10;
RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postDetails.setRetryPolicy(policy);
//Creating requestqueue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request queue
requestQueue.add(postDetails);
Log.d(TAG, "Data sent is " + comment + name + email);
} else {
Log.d(TAG, "All fields are not valid");
}
}
重复问题将在本线程中解释
只要更改请求有这个重试策略就可以了
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
我在我的应用程序中使用凌空网络库。 问题在于,当网络连接缓慢时,它会多次发送数据。 在我搜索了这个问题后,我能找到的关于这个问题的信息如下: 但是我不能编辑我的截击库Hurkstack类。 它说: 这个类文件的jar属于容器android私有库,它不允许修改它条目上的源附件。 我该怎么办有人能帮我吗 我有下面的代码,我应该在哪里修改。
当我做POST请求调用使用JsonObjectRequest在凌空然后在某些wifi它是发送空的身体。但是它可以很好地处理StringRequest POST请求。它在所有移动网络上都运行良好。 我正在使用node。js服务器和expressjs/body解析器。当我使用邮递员发出邮件请求时,一切正常。 错误是什么?如果有人想看我能提供的代码。 使用StringRequest的POST请求 POS
我试图使用Android Volley库发送JSON Post请求,但我似乎没有得到json的正文,我在我的Web服务器上得到未定义的正文参数。我需要json的参数体是一个单一的对象"name=某些Val } 我也在上面的代码中尝试了这一点,但没有成功:
我看了关于截击的Google IO 2013课程,我正在考虑改用截击。Volley是否支持向请求添加POST/GET参数?如果是,我怎么做?
我有curl请求从Android推送数据到服务器。下面是Curl请求: 我应该如何使用截击进行post请求?我很困惑,因为我不知道该放在哪里: 所以请任何人帮我完成这件事。任何答案都是可贵的。非常感谢。安静!
我正在使用截击与API交互。我需要向返回JSON数组的服务发送post请求(带参数)。 JsonObjectRequest有一个构造函数,它接受一个方法和一组参数 但是JSONArrayRequest(我需要的一个)只有一个构造函数的形式 我如何才能使其发送包含数据的POST请求?