【问题标题】:Connect Laravel App to Android将 Laravel 应用程序连接到 Android
【发布时间】:2016-07-22 15:10:30
【问题描述】:

我构建了一个小型 laravel 应用程序,它只有一个表单并将数据保存到数据库(phpmyadmin)。现在我尝试在我的 android 应用程序中显示该数据,我尝试通过 ip 获取它。

主要活动如下所示:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onConnect(View view) {
        new Thread(){
            public void run(){
                HttpClient myClient = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://192.168.145.33:8000/gaIndex");
                try {
                    List<NameValuePair> myArgs = new ArrayList<NameValuePair>();
                    myArgs.add(new BasicNameValuePair("email", "test@hotmail.de"));
                    myArgs.add(new BasicNameValuePair("password", "test"));
                    post.setEntity(new UrlEncodedFormEntity(myArgs));
                    HttpResponse myResponse = myClient.execute(post);
                    BufferedReader br = new BufferedReader( new InputStreamReader(myResponse.getEntity().getContent()));
                    String line = "";
                    while ((line = br.readLine()) != null)
                    {
                        Log.d("mytag", line);

                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
}

如果我启动应用程序并按下按钮连接控制台给我一堆 html...

我也在控制台输出中得到了这个:

令牌不匹配异常 在 VerifyCsrfToken.php 第 53 行:

有谁知道我做错了什么。

【问题讨论】:

  • 您使用的是 L 5.1 还是 5.2?
  • 您应该在这里使用 RESTful 路由模式,而不是直接尝试通过 Android 应用访问视图。

标签: php android database api laravel


【解决方案1】:

Laravel 有一个 verifyCsrf 中间件,它适用于所有传入的请求,以保护应用程序免受Cross Site Request Forgery

您可以通过将路由添加到 VerifyCsrfToken 类中的 $except 数组来绕过此问题:

在app/Http/Middleware/VerifyCsrfToken.php中

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'gaIndex'
    ];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2021-09-12
    • 2015-10-17
    • 2015-06-05
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    相关资源
    最近更新 更多