连接android与php mysql

时间:2017-03-15 14:33:48

标签: php android mysql xampp

我已经将我的Android应用程序连接到运行在000.webhost.com上的php mysql xampp服务器,我面临的问题是php脚本给我错误的响应,因为我是php新手,所以我无法找出问题

这是我的登录脚本:

<?php
$con = mysqli_connect("localhost", "id1083203_root", "root1", "id1083203_subway");
if ($con){
echo 'connected';

}
    $email = isset($_POST['em']) ? $_POST['em'] : "";
    $password =isset($_POST['pwd']) ? $_POST['pwd'] : "";
   $role = isset($_POST['item']) ? $_POST['item'] : "";


$statement = mysqli_prepare($con, "SELECT * FROM login WHERE email = ? AND password = ? AND role=?");
mysqli_stmt_bind_param($statement, "sss", $email, $password, $role);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id, $email, $password, $role);

$response = array();
$response["success"] = false;  

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;  
    $response["email"] = $email;
    $response["password"] = $password;
   $response["role"] = $role;

}

echo json_encode($response);
?>

这是我的android端请求代码

package yfsoftwares.foodorderingsystem;

/**
 * Created by user on 15/03/2017.
 */

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Login extends AppCompatActivity implements OnItemSelectedListener {
String item;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_login);


    // Spinner element
    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    // Spinner click listener
    spinner.setOnItemSelectedListener(this);


    Button btnlogin=(Button)findViewById(R.id.login);

    final EditText email=(EditText) findViewById(R.id.mail);
    final EditText password=(EditText) findViewById(R.id.pwd);

    TextView reg = (TextView)findViewById(R.id.reg);
    reg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent nxt = new Intent(getApplicationContext(), RegisterActivity.class);
            startActivity(nxt);
        }
    });

    TextView fp = (TextView) findViewById(R.id.forgot_password);

    fp.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent nxt = new Intent(getApplicationContext(), ForgotPasswordActivity.class);
            startActivity(nxt);
        }
    });


    // Spinner Drop down elements
    List<String> role = new ArrayList<String>();
    role.add("admin");
    role.add("cook");
    role.add("customer");

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, role);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);

    btnlogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // get The User name and Password
            String em=email.getText().toString();
            String pwd=password.getText().toString();

            // Response received from the server
            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        Toast.makeText(Login.this, "String is"+response, Toast.LENGTH_LONG).show();
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success) {

                            Intent intent = new Intent(Login.this, welcome.class);
                            startActivity(intent);

                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
                            builder.setMessage("Login Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };


            Toast.makeText(Login.this, "creds are"+em+pwd+item, Toast.LENGTH_LONG).show();
            LoginRequest loginRequest = new LoginRequest(em, pwd,item, responseListener);
            RequestQueue queue = Volley.newRequestQueue(Login.this);
            queue.add(loginRequest);
        }
    });
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    item = parent.getItemAtPosition(position).toString();

    // Showing selected spinner item
    //  Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}}

1 个答案:

答案 0 :(得分:0)

尝试这个我想它会起作用

<?php
$con = mysqli_connect("localhost", "id1083203_root", "root1", "id1083203_subway");
if ($con){
echo 'connected';
$email = isset($_POST['em']) ? $_POST['em'] : "";
    $password =isset($_POST['pwd']) ? $_POST['pwd'] : "";
   $role = isset($_POST['item']) ? $_POST['item'] : "";


$statement = mysqli_prepare($con, "SELECT * FROM login WHERE email = ? AND password = ? AND role=?");
mysqli_stmt_bind_param($statement, "sss", $email, $password, $role);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id, $email, $password, $role);

$response = array();
$response["success"] = false;  

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;  
    $response["email"] = $email;
    $response["password"] = $password;
   $response["role"] = $role;

}

echo json_encode($response);

}
else
{
    echo "connection failed";
}

?>