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.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2FoLGhr
via IFTTT
Aucun commentaire:
Enregistrer un commentaire