我正在尝试在Laravel和Volley上执行POST请求,但同样的错误已经出现了一段时间了。对于这个例子,我试图通过创建一个JSONObject来做到这一点;然后试着在Laravel上接收它。
这是我在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;
}
}
}
然后我试图像这样在laravel上获取JSON对象:
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();
}
}
以防万一,这是我的模特:
<?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'];
}
我还在VerifyCsrfToken.php上添加了一个例外:
protected $except = [
//
'http://dev.com/VolleyPost',
];
起初我得到了一个TimeOutError,它是在实现最后一段代码后解决的。不过,我无法摆脱这个错误,
03-05 20:17:58.989 14139-14178 / com.example.umi.volley1 D / Volley: [1174] BasicNetwork.logSlowRequests:请求的HTTP响应=&lt; [] http://192.168。。:80 / VolleyPost 0x67692ab4 NORMAL 1&gt; [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:意外的响应代码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
为了让一切顺利。我错过了什么吗?我的代码有问题吗?我将不胜感激,谢谢。