【问题标题】:send json to Laravel using postman and android使用 postman 和 android 将 json 发送到 Laravel
【发布时间】: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


【解决方案1】:

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

首先你必须从https://www.getpostman.com/下载邮递员来测试你的API是否真的工作。

使用邮递员创建一个发布请求。确保您遵循以下示例数据

确保您设置了与控制器相关联的路由

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

此外,如果您尝试将 POST 数据发送到 laravel,默认情况下,如果您要使用 laravel 的 MVC,他们会提供适用于表单的 CSRF 令牌。与此同时,我们将把它记下来并发表评论。只需转到 app/http/kernel.php

现在您将从之前的代码中得到以下结果

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

我们测试了我们能够向 laravel 发送数据。我希望这真的有帮助。

【讨论】:

    【解决方案2】:

    如果你想发送数据,你会想在你的邮递员上使用 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

    【讨论】:

    • 我更改为 POST 但 ger 错误:E/Volley: [306] BasicNetwork.performRequest: 192.168.1.4:8000/r D/error: com.android.volley.ClientError 的意外响应代码 405(在 android 中)
    • 抱歉,将 GET 更改为 POST 我得到错误:E/Volley: [278] BasicNetwork.performRequest: Unexpected response code 419 for 192.168.1.4:8000/r D/error: com.android.volley.ClientError
    • 这意味着服务器已找到并响应,但我认为问题与定义您发送的数据的 Header Content-Type 有关。在邮递员中验证您正在使用 application/json。如果您不知道如何在邮递员中更改标题,请阅读此处的标题部分link
    猜你喜欢
    • 2016-02-13
    • 2020-09-04
    • 2019-08-30
    • 2015-08-01
    • 2012-09-26
    • 2018-06-30
    • 2015-06-25
    • 2020-09-24
    • 2015-07-21
    相关资源
    最近更新 更多