【问题标题】:Restful API. Cannot add data from Android app to MySQL database using Laravel宁静的 API。无法使用 Laravel 将数据从 Android 应用程序添加到 MySQL 数据库
【发布时间】:2021-07-01 12:44:59
【问题描述】:

我正在尝试使用将数据从 android 应用程序添加到 mysql 数据库 RxJava 和 Laravel 用于 RestFul API。我无法将数据添加到数据库中,我认为我的 php 代码不正确。下面是我的一些 POST 方法代码。

StudentController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller
{       
    function addData(Request $req) {
        $st = new Student;
        $st->name = $req->name;
        $st->age = $req->age;
        if ($st->save()) {
            echo "Add 1 field success !";
        } 
    } 
}

JsonApi.kt

package com.example.demo9

import io.reactivex.Observable
import retrofit2.Response
import retrofit2.http.*

interface JsonApi {
    
    // For laravel

    @FormUrlEncoded
    @POST("add2")
    fun insertStudent(@Field("name") name: String, @Field("age") age: Int):
            Observable<Response<Void>>

}

【问题讨论】:

    标签: php android laravel rx-java


    【解决方案1】:

    更新:

    问题已经被我解决了。 通过将 url 声明添加到 app/Http/Middleware/VerifyCsrfToken.php 文件中:

    <?php
    
    namespace App\Http\Middleware;
    
    use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
    
    class VerifyCsrfToken extends Middleware
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        
        protected $except = [
            'api/add2/*'
        ];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-20
      • 2016-04-17
      • 1970-01-01
      • 2023-03-13
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多