abd*_*him 9 android android-volley
每次我尝试使用Volley的POST方法时,都会出现严重错误.我在getCause中得到null值,在getNetworkResponse.toString()中得到一些默认值.
如果我使用GET方法,这工作正常(我从我的网址得到回复).
有人可以帮忙吗?
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("teststr", "abd");
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
url,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(), "Success"+response.toString(), Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "JSON ERROR", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("abd", "Error: " + error
+ ">>" + error.networkResponse.statusCode
+ ">>" + error.networkResponse.data
+ ">>" + error.getCause()
+ ">>" + error.getMessage());
}
}) {
@Override
protected Map<String,String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("key", "value");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
requestQueue.add(request);
Run Code Online (Sandbox Code Playgroud)
错误日志:
错误:错误:com.android.volley.ServerError >> 404 >> [B @ 42b1e0d0 >> null >> null
更新: networkResponse.statusCode为404,尽管url是可访问的(如果我只使用GET方法则返回数据).如果我在POST方法中删除标题部分,仍然是相同的.
网址:
<?php
$response = array();
$jsonString = file_get_contents('php://input');
$jsonObj = json_decode($jsonString, true);
if(!isset($jsonObj['teststr'])){
$response["msg"] = "No data.";
}else{
$response["msg"] = "Success: ".$jsonObj['teststr'];
}
echo json_encode($response);
?>
Run Code Online (Sandbox Code Playgroud)
首先,尝试确保您的服务器运行良好.您可以使用Postman(chrome插件)或任何其他方式向网址发送帖子请求并查看其响应内容.
确保您的服务器没有问题后,让我们用排球解决问题.
JsonObjectRequest使用POST方法时有一些问题.像这样Volley JsonObjectRequest Post请求不起作用.
我建议你StringRequest先使用并getParams像之前一样覆盖方法.在您完成此任务后,您可以尝试编写自己的请求,这不是非常困难但非常有用.
我也建议添加request.setShouldCache(false)之前requestQueue.add(request);.默认情况下,volley将响应保存在其缓存中,此行为可能会导致一些奇怪的问题.
小智 5
问题是您的队列。更改您的齐射代码为:
RequestQueue queue = Volley.newRequestQueue(this);
String URL = EndPoints.BASE_URL + "/call";
StringRequest request = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
Log.d("onResponse", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse response = error.networkResponse;
String errorMsg = "";
if(response != null && response.data != null){
String errorString = new String(response.data);
Log.i("log error", errorString);
}
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("key_1","value_1");
params.put("key_2", "value_2");
Log.i("sending ", params.toString());
return params;
}
};
// Add the realibility on the connection.
request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));
// Start the request immediately
queue.add(request);
Run Code Online (Sandbox Code Playgroud)
和您的php(laravel)代码:
$response['success'] = true;
$response['user']['tell'] = $user->tell;
$response['user']['code'] = $user->code;
$response['user']['time'] = $time;
$response['user']['register_state'] = '1'
return response()->json($response, 200);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41892 次 |
| 最近记录: |