duanchou6534 2018-03-06 02:47
浏览 165

Laravel-Volley:尝试POST方法时出现意外的响应代码419

I am trying to perform a POST request on Laravel and Volley, but the same error has been appearing for some time now. For this example, I'm trying to do so by creating a JSONObject; then trying to receive it on Laravel.

This is my code on Android Studio:

public class Register extends AppCompatActivity implements View.OnClickListener {
EditText name, surname, phone;
Button btn;
String url = "http://192.168.*.*:80/VolleyPost";
RequestQueue requestQueue;

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

    btn = (Button) findViewById(R.id.post);
    btn.setOnClickListener(this);
    name= (EditText) findViewById(R.id.edtname);
    surname= (EditText) findViewById(R.id.edtsn);
    phone= (EditText) findViewById(R.id.edtphone);
    requestQueue = Volley.newRequestQueue(this);

}

@Override
public void onClick(View view)
{
    switch(view.getId()) {
        case R.id.post: {
            JSONObject object = new JSONObject();
            try {
                object.put("Name", name.getText().toString());
                object.put("Surname", surname.getText().toString());
                object.put("Phone", phone.getText().toString());
                Log.d("Object", "" + object);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            JsonObjectRequest jsonrequest = new JsonObjectRequest(Request.Method.POST, url, object,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response)
                        {
                            Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
                            Log.d("Response", ""+response);
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error)
                        {
                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                            Log.e("VOLLEY", ""+error);
                        }
                    });
            jsonrequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            requestQueue.add(jsonrequest);
            break;
        }
    }

}

Then I'm trying to fetch the JSON object on laravel like this:

web.php:

Route::post('/VolleyPost', 'PublicController@addPerson');

PublicController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Person;

class PublicController extends Controller
{
    public function addPerson(Request $request)
    {
        $person= new Person();

        $person->Name= $request->Name;
        $person->Surname = $request->Surname;
        $person->Phone = $request->Phone;
        $person->save();
    }
}

Just in case, this is my model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
    protected $table = 'persons';
    public $timestamps=false;
    protected $primary_key='id';
     protected $fillable = ['Name', 'Surname', 'Phone'];

}

I've as well added an exception on the VerifyCsrfToken.php:

protected $except = [
        //
        'http://dev.com/VolleyPost',
    ];

At first I was getting a TimeOutError, which was solved after implementating the last bit of code. Nevertheless, I can't get rid of this error,

03-05 20:17:58.989 14139-14178/com.example.umi.volley1 D/Volley: [1174] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168..:80/VolleyPost 0x67692ab4 NORMAL 1> [lifetime=8146], [size=1518], [rc=419], [retryCount=0] 03-05 20:17:58.989 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x4E 03-05 20:17:58.989 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x4F 03-05 20:17:58.989 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x4E 03-05 20:17:58.991 14139-14178/com.example.umi.volley1 E/Volley: [1174] BasicNetwork.performRequest: Unexpected response code 419 for http://192.168..:80/VolleyPost 03-05 20:17:58.992 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x52 03-05 20:17:58.995 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x4E 03-05 20:17:58.995 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x4E 03-05 20:17:58.995 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x4E 03-05 20:17:58.995 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x4B 03-05 20:17:58.996 14139-14178/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x6C 03-05 20:17:58.999 14139-14139/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x52 03-05 20:17:59.000 14139-14139/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x57 03-05 20:17:59.000 14139-14139/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x47 03-05 20:17:59.002 14139-14139/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x49 03-05 20:17:59.002 14139-14139/com.example.umi.volley1 D/jdwp: sendRequest : Len=0x49 03-05 20:17:59.016 14139-14139/com.example.umi.volley1 E/VOLLEY: com.android.volley.ClientError

in order to make everything work. Am I missing something? Is something wrong with my code? I would appreciate any help, thank you.

  • 写回答

1条回答 默认 最新

  • dounong5373 2018-05-11 04:24
    关注

    Add the below url in the VerifyCsrfToken.php and try.

    URL:

    http://192.168.*.*:80/VolleyPost
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?