我将使用laravel 5.1进行数据库连接,并从Android发送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 路由
<?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);
}
}
laravel.log
文件,另外看起来你的安卓代码没有发送到正确的URL路径。 - Neo