Alex Oxilg
Alex Oxilg

Reputation: 121

Connect Laravel App to Android

I built a small laravel application which just has a form and saves data to the database(phpmyadmin). Now I tried to display that data in my android application, I tried to get it through the ip.

Main Activity looks like this:

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", "[email protected]"));
                    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();

    }
}

If i start the app and push the button to connect the console give me bunch of html...

I also got this in the console output:

TokenMismatchException in VerifyCsrfToken.php line 53:

Does anybody know what I am doing wrong.

Upvotes: 1

Views: 2025

Answers (1)

Angad Dubey
Angad Dubey

Reputation: 5452

Laravel has a verifyCsrf middleware that is applied to all incoming requests to protect the application from Cross Site Request Forgery

You can make routes bypass this by adding them to the $except array in the VerifyCsrfToken class:

In 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'
    ];
}

Upvotes: 2

Related Questions