试图将Android排球发送请求发送到Laravel

时间:2016-02-09 11:27:37

标签: android laravel android-volley

我使用laravel 5.1进行数据库连接,并将post请求表单发送到laravel .. 但例外是显示“服务器错误”

我的Android代码: -

package com.example.ankit.postrequest;

import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class DataSent extends AppCompatActivity {

    private static final String KEY_USERNAME ="rohit" ;
    private static final String KEY_PASSWORD ="1234" ;
    private static final String KEY_EMAIL ="a@gmail.com" ;
    private static final String KEY_token ="xyz" ;
    EditText Etname,Etpass,Etmail,Et_token;
    RequestQueue insert;
    String insertUrl ="http://memorableshaadi.com/j";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Etmail = (EditText)findViewById(R.id.activity_booking_form_email);
        Etname = (EditText)findViewById(R.id.activity_booking_form_name);
        Etpass = (EditText)findViewById(R.id.activity_booking_form_dow);
        Et_token = (EditText)findViewById(R.id.activity_booking_form_token);
    }

    public void call(View v) {
          registerUser();
    }

    private void registerUser(){
        final String username = Etname.getText().toString().trim();
        final String password = Etpass.getText().toString().trim();
        final String email = Etmail.getText().toString().trim();
        final String token = Ettoken.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, insertUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(DataSent.this, response, Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(DataSent.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);
                params.put(KEY_EMAIL, email);
                params.put(KEY_token, token);
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

}

Laravel routes.php

<?php

Route::post('users','JsonController@data');

laravel JsonController.php

   <?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use File;
use Storage;
use response;
use Illuminate\Contracts\Routing\ResponseFactory;
class JsonController extends Controller{
public function data(Request $dat){
            $user = $dat->username;
            $pass = $dat->password;
            $email = $dat->email;
            DB::table('user')->insert([
                    'user_name' => $user,
                    'user_email' => $pass, 
                    'user_password' => $email
            ]);
            echo($user.$pass.$email);
        }
}

1 个答案:

答案 0 :(得分:2)

您应该注意两件事: 第一:您面临的错误是: VerifyCsrfToken.php中的TokenMismatchException,原因是因为建立一个身份验证任何请求应该通过你的中间件,并且必须传递Laravel令牌,所以因为你使用post方法你没有通过它并且它已经丢失你的请求所以解决这个问题的最简单方法是为你的App \ Http \ Middleware \ VerifyCsrfToken.php添加例外

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

您的请求也会正常运行,您也可以使用此库来处理令牌身份验证jwt-auth。 但是我应该提一下你的代码: 在你的Android代码中:

private static final String KEY_USERNAME ="rohit" ;
private static final String KEY_PASSWORD ="1234" ;
private static final String KEY_EMAIL ="a@gmail.com" ;
private static final String KEY_token ="xyz" ;

这些键将用于将请求的值与检查函数的值进行匹配,因此您应该首先将它们重命名为API源,否则检查功能将无法获得来自任何请求的值 所以他们应该这样:

private static final String KEY_USERNAME ="username" ;
private static final String KEY_PASSWORD ="password" ;
private static final String KEY_EMAIL ="email" ;

我希望这会对你有所帮助)