无法将变量值从android发送到php

时间:2014-04-14 09:16:58

标签: php android

我想将edittext中的用户名发送到php代码,然后将其插入到表格中。到目前为止,我的代码无法执行此操作.code如下:

loginstudent.java

public class LoginStudent extends Activity {
    Button b1;
    EditText e1, username;@
    Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginstudent);
        b1 = (Button) findViewById(R.id.button1);
        e1 = (EditText) findViewById(R.id.editText1);
        username = (EditText) findViewById(R.id.phone_no);
        b1.setOnClickListener(new View.OnClickListener() {@
            Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (e1.getText().length() == 0 || username.getText().length() == 0) {
                    Toast.makeText(getBaseContext(), "Please Enter all the fields!!", Toast.LENGTH_LONG).show();
                } else {
                    //Toast.makeText(getBaseContext(), "Registered successfully!", Toast.LENGTH_LONG).show();
                    Intent i = new Intent(LoginStudent.this, Verify.class);
                    startActivity(i);
                }
            }
        });
    }
    public void login(View view) {
        String username1 = username.getText().toString();
        new SigninActivity(this).execute(username1);
    }
}

signinactivity.java

public class SigninActivity extends AsyncTask < String, Void, String > {
    private Context context;
    public SigninActivity(Context context) {
        this.context = context;
    }
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String...arg0) {
        try {
            String username = (String) arg0[0];
            String link = "http://example.com/try1.php?username=" + username;
            URL url = new URL(link);
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(link));
            HttpResponse response = client.execute(request);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            while ((line = in .readLine()) != null) {
                sb.append(line);
                break;
            } in .close();
            return sb.toString();
        } catch (Exception e) {
            return new String("Exception: " + e.getMessage());
        }
    }

    @SuppressLint("NewApi")@ Override
    protected void onPostExecute(String result) {
        if (result.isEmpty()) {
            System.out.println("user not present");
        } else {
            System.out.println(result);
        }
    }
}

try1.php

<?php
class pswd {
    function generatePassword($length, $strength) {
        $vowels     = 'aeuy';
        $consonants = 'bdghjmnpqrstvz';
        if ($strength & 1) {
            $consonants .= 'BDGHJLMNPQRSTVWXZ';
        }
        if ($strength & 2) {
            $vowels .= "AEUY";
        }
        if ($strength & 4) {
            $consonants .= '23456789';
        }
        if ($strength & 8) {
            $consonants .= '@#$%';
        }

        $password = '';
        $alt      = time() % 2;
        for ($i = 0; $i < $length; $i++) {
            if ($alt == 1) {
                $password .= $consonants[(rand() % strlen($consonants))];
                $alt = 0;
            } else {
                $password .= $vowels[(rand() % strlen($vowels))];
                $alt = 1;
            }
        }
        return $password;
    }
}
$con = mysqli_connect("example.com", "abc", "1234", "pqrs");
if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = (int) $_GET['username'];
echo $username;
$result = mysqli_query($con, "SELECT * from login_stud where phone_no = '$username'");
if ($row = mysqli_fetch_array($result)) {
    echo "  user present";
} else {
    $p      = new pswd();
    $passwd = $p->generatePassword(4, 0);
    $result = mysqli_query($con, "Insert into login_stud (phone_no,password)values       ('$username','$passwd')");
}
mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:0)

<?php
$con = mysqli_connect("example.com", "abc", "1234", "pqrs");
if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//$username = (int) $_GET['username'];
$username = $_GET['username'];
echo $username;
$result = mysqli_query($con, "SELECT * from login_stud where phone_no = '$username'");
if ($row = mysqli_fetch_array($result)) {
    echo "  user present";
} else {
    $p      = new pswd();
    $passwd = $p->generatePassword(4, 0);
    $result = mysqli_query($con, "Insert into login_stud (phone_no,password)values       ('$username','$passwd')");
}
mysqli_close($con);
?>

您以字符串类型发送用户名,但在检索时这是int类型。所以它的价值是
总是0。