无法从php到android检索json格式的数据

egdjgwm8  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(354)

它是一个寄存器函数。我可以将记录添加到我的数据库中。但是对于通过php脚本做出的响应,我无法显示响应,也无法从php获得对android的响应。
当邮件被复制时,php应该以fail。。。。我怎样才能在android上显示它呢?
单文件

<?php
include_once 'connection.php';

class User {

private $db;
private $connection;

function __construct() {
    $this -> db = new DB_Connection();
    $this -> connection = $this->db->getConnection();
}

public function does_user_exist($name,$tel_no,$contact_point,$email,$address,$logo_url, $operation_time,$encrypted_password)
{
    $query = "Select * from shop where email='$email'  ";
    $result = mysqli_query($this->connection, $query);
    if(mysqli_num_rows($result)>0){
        $json['fail'] = ' Already registered '.$email;
        echo json_encode($json);
        mysqli_close($this -> connection);
    }else{
        $query = "insert into shop (name, tel_no, contact_point, email, address, logo_url, operation_time, password) values ( '$name','$tel_no','$contact_point','$email','$address','$logo_url','$operation_time','$encrypted_password')";
        $inserted = mysqli_query($this -> connection, $query);
        if($inserted == 1 ){
            $json['success'] = 'Account created';
        }else{
            $json['error'] = 'Wrong password';
        }
        echo json_encode($json);
        mysqli_close($this->connection);
    }

}

 }

$user = new User();

 ..........

寄存器.java

public class Register extends AppCompatActivity {private EditText nam, teln, 
 contactpoin, emai, addres, logour, operationtim, pw1 , pw2;
private Button btnregister;
private RequestQueue requestQueue;
private static final String Url  = "http://XXXXXX/stampe/register.php";
private StringRequest request;

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

    StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(threadPolicy);

    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();

    nam = (EditText) findViewById(R.id.name);
    teln = (EditText) findViewById(R.id.tel_no);
    contactpoin = (EditText) findViewById(R.id.contact_point);
    emai = (EditText) findViewById(R.id.email);
    addres = (EditText) findViewById(R.id.address);
    logour = (EditText) findViewById(R.id.logo_url);
    operationtim = (EditText) findViewById(R.id. operation_time);
    pw1 = (EditText) findViewById(R.id. password);
    pw2 = (EditText) findViewById(R.id. pwcheck);
    btnregister = (Button) findViewById(R.id.btn_register);

    requestQueue = Volley.newRequestQueue(this);

    btnregister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(nam.getText().length()==0 ||teln.getText().length()==0 ||contactpoin.getText().length()==0 ||emai.getText().length()==0 ||pw1.getText().length()==0 ||pw2.getText().length()==0   )
            {
                btnregister.setError("請輸入資料!");
            }else if(!pw1.getText().toString().equals(pw2.getText().toString())){
                System.out.println(pw1.getText().toString());
                System.out.println(pw2.getText().toString());
                pw1.setError("兩次輸入的密碼不一致");
            }
            else{

            request = new StringRequest(Request.Method.POST, Url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        System.out.print(jsonObject);
                        if(jsonObject.names().get(0).equals("success")){
                            Toast.makeText(getApplicationContext(),"SUCCESS "+jsonObject.getString("success"),Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(),MainActivity.class));
                        }else {
                            Toast.makeText(getApplicationContext(), "Error" +jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                            startActivity(new Intent(getApplicationContext(),MainActivity.class));
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                   HashMap<String,String> hashMap = new HashMap<String, String>();

                    hashMap.put("name",nam.getText().toString());
                    hashMap.put("tel_no",teln.getText().toString());
                    hashMap.put("contact_point",contactpoin.getText().toString());
                    hashMap.put("email",emai.getText().toString());
                    hashMap.put("address",addres.getText().toString());
                    hashMap.put("logourl",logour.getText().toString());
                    hashMap.put("operation_time",operationtim.getText().toString());
                    hashMap.put("password",pw1.getText().toString());
                    return hashMap;
                }
            };requestQueue.add(request); }}});}}
rsaldnfx

rsaldnfx1#

试试这个

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$database = "mydb";

$connection = mysqli_connect($servername,$username,$password,$database);

if (!$connection) {
    die("Connection Failed" .mysqli_connect_error());
}
else 
    echo "Connection Sucessful"

//$data = $_POST["data"];
$stat = $connection->prepare("SELECT name, tel_no, contact, email, address, 
logo_url ,operation_time, encrypted_password from mytable");

$stat->execute();

$stat ->bind_result($name, $tell_no, $contact, $email, $address, 
$logo_url ,$operation_time, $encrypted_password);

$data = array();

while ($stat->fetch()) {

    $temp = array();
    $temp['name'] = $name;
    $temp['tel_no'] = $tel_no;
    $temp['contact'] = $contact;
    $temp['email'] = $email;
    $temp['address'] = $address;
    $temp['logo_url'] = $logo_url;
    $temp['operation_time'] = $operation_time;
    $temp['encrypted_password'] = $encrypted_password;

    array_push($data, array("data"=>$temp));
}

echo json_encode($data);

?>

相关问题