我构建了一个小的 laravel 应用程序,它只有一个表单并将数据保存到数据库 (phpmyadmin)。现在我试图在我的 android 应用程序中显示该数据,我试图通过 ip 获取它。
主要 Activity 如下所示:
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...
我也在控制台输出中得到了这个:
TokenMismatchException in VerifyCsrfToken.php line 53:
有谁知道我做错了什么
最佳答案
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'
];
}
关于php - 将 Laravel 应用程序连接到 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379573/