使用邮递员和android将json发送到Laravel

时间:2019-03-02 16:13:06

标签: java php android json laravel

我正在尝试使用邮递员将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使用齐射来发送数据,因此检查了Laravel日志:

Column 'email' cannot be null (this is Laravel logs)

以及android Logs:

E / Volley:[299] BasicNetwork.performRequest:http://192.168.1.4:8000/r的意外响应代码500 D /错误:com.android.volley.ServerErro

我的android代码是:

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);
    }
}

2 个答案:

答案 0 :(得分:0)

当您要发送数据时,您将要在邮递员上使用POST或PUT方法,特别是在发送正文时,这意味着您正在发送数据。 Get方法用于从服务检索数据。 查看CRUD functions了解更多信息。 您的邮递员应该看起来像this

最后在您的android代码中尝试更改此行

JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, "http://192.168.1.4:8000/r",requestJsonObject , new Response.Listener<JSONObject>() {

使用Request.Method.POST

答案 1 :(得分:0)

我希望这有助于尝试搜索如何将JSON数据发送到laravel的人们,不仅针对Android应用程序,还针对所有人。该解决方案的目标是确定是否可以向laravel发送JSON数据。

首先,您必须从https://www.getpostman.com/下载邮递员,以测试您的API是否确实有效。

使用邮递员创建发帖请求。确保您遵循以下示例数据 enter image description here

请确保您设置了与控制器关联的路由enter image description here

这是控制器部分,它将显示您发送的JSON数据是否被成功接受。 enter image description here

此外,如果您尝试将POST数据发送到laravel,则默认情况下,它们将提供CSRF令牌,如果您要使用laravel的MVC,则该CSRF令牌适用于表单。同时,我们将对此予以删除并予以注释。只需转到app / http / kernel.php enter image description here

现在您将从前面的代码中获得以下结果

$json = json_decode($request['json']);
echo $json->{'email'};
echo "\n";
echo $json->{'password'};

enter image description here 我们测试了是否可以将数据发送到laravel。我希望这确实有帮助。