【发布时间】:2017-12-22 03:06:36
【问题描述】:
我正在尝试将一些数据从我的 Android 应用程序发送到 Laravel 应用程序。如果我使用邮递员测试 Laravel API,所有的帖子数据都会被正确接收。但是,当从 Android 调用 API 时,Laravel 应用会收到一个空请求,并且 POST 参数是空的:
安卓:
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.v("TAG",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.v("TAG",error.toString());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("user","ASDASDADSAS");
params.put("pass","ASDASDADSAS");
params.put("comment", "ASDASDADSAS");
return params;
}
@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;
}
};
queue.add(sr);
回复:
<pre></pre><pre></pre><pre></pre>{"status":"500","data":[],"message":"**** parameter is either missing or empty"}
1,500 是硬编码的,不是服务器实际抛出的。
2,在创建 test.php 文件并回显 $_POST 时,在 laravel 之外接收到 post 数据。
Laravel: - 路由被添加到 csrf 异常中。
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class APIController extends Controller
{
public function postitems(Request $request){
echo "<pre>";
print_r($request->input('user')); // null
echo "</pre>";
echo "<pre>";
print_r($request->all()); // null
echo "</pre>";
$rawPostData = file_get_contents("php://input");
echo "<pre>";
print_r($rawPostData); //null
echo "</pre>";
//handles request and api Call and gives response if all the data is present
}
}
【问题讨论】:
标签: php android laravel-5 android-volley