【发布时间】:2019-07-24 10:05:23
【问题描述】:
我正在尝试使用邮递员将 json 发送到 Lavavel,但我遇到了这个错误。
enter image description here 这是我的 json 代码:
{
"email":"test@test.com",
"password":"testtest"
}
这是 Laravel 代码:
Route::get('/r','test@store');
和
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Log;
class test extends Controller
{
public function store(Request $request)
{
$email = $request->input('email');
$password = $request->input('password');
Log::info('test');
Log::info($email);
Log::info($password);
DB::table('login')->insert([
['email' => $email],
['password' => $password]
]);
}
}
我也尝试使用 android 使用 volley 发送数据,因此检查了 Laravel 日志:
Column 'email' cannot be null (this is Laravel logs)
在安卓日志上:
E/Volley:[299] BasicNetwork.performRequest:http://192.168.1.4:8000/r 的意外响应代码 500 D/错误:com.android.volley.ServerErro
我的安卓代码是:
public class ApiService {
private final Context context;
public ApiService(Context context){
this.context=context;
}
public void loginUser(String email, String password, final OnLoginResponse onLoginResponse){
JSONObject requestJsonObject=new JSONObject();
try {
requestJsonObject.put("email",email);
requestJsonObject.put("password",password);
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, "http://192.168.1.4:8000/r",requestJsonObject , new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("response",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error",error.toString());
}
}) {
@Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Content-Type", "application/json");
return headers;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(18000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Volley.newRequestQueue(context).add(request);
} catch (JSONException e) {
Log.e(TAG, "loginUser: "+e.toString());
}
}
public interface OnLoginResponse{
void onResponse(boolean success);
}
}
【问题讨论】:
-
你的 laravel ROUTE 方法应该是 POST 来发送数据。我看到你正在为 POSTMAN 使用 GET 方法
-
我将 ROUTE 更改为 post 但得到错误 419 未知状态(在邮递员上),在 android 上得到 E/Volley: [306] BasicNetwork.performRequest: Unexpected response code 405 for 192.168.1.4:8000/r D/error : com.android.volley.ClientError
-
@mohammadmghi 如果出现 419 错误,可能是您的 post 请求没有 csrf 令牌。
-
这条路由
Route::get('/r','test@store');是在api.php还是web.php中定义的? -
@ViperTecPro in web.php
标签: java php android json laravel