【问题标题】:Android coding with mysql and php使用 mysql 和 php 进行 Android 编码
【发布时间】:2015-01-17 11:43:57
【问题描述】:

如何将数据从这里传递给传递特定数据的下一个活动?我在 Android 上使用 PHP+MySQL 和 Java。

我通过在按钮上使用 onClick 来调用它:

public class LoginActivity extends Activity {
Handler h = new Handler();

/*
 * Login
 */
public void Login(View view) {

    EditText usernameField = (EditText)findViewById(R.id.editText_Username_L);
    EditText passwordField = (EditText)findViewById(R.id.editText_Password_L);

    String username = usernameField.getText().toString();
    String password = passwordField.getText().toString();
    new SigninActivity(this).execute(username,password);
}

/*
 * Login
 */

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    //int id = item.getItemId();
    //if (id == R.id.action_settings) {
    //  return true;
    //}
    return super.onOptionsItemSelected(item);
}
}

这是我的代码传递和获取数据的地方:

public class SigninActivity extends AsyncTask<String,Void,String> {

private TextView roleField;
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 password = (String)arg0[1];
        String link="http://192.168.254.108/Login/login.php";
        String data  = URLEncoder.encode("username", "UTF-8")
                + "=" + URLEncoder.encode(username, "UTF-8");
        data += "&" + URLEncoder.encode("password", "UTF-8")
                + "=" + URLEncoder.encode(password, "UTF-8");
        URL url = new URL(link);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter
                (conn.getOutputStream());
        wr.write( data );
        wr.flush();
        BufferedReader reader = new BufferedReader
                (new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        // Read Server Response
        while((line = reader.readLine()) != null)
        {
            sb.append(line);
            break;
        }
        return sb.toString();
    }catch(Exception e){
        return new String("Exception: " + e.getMessage());
    }
}
@Override
protected void onPostExecute(String result){

    this.roleField.setText(result);
    if(result.equals("")){
        Intent i = new Intent(SigninActivity.this, DashboardActivity.class);
        i.putExtra("user_id", result);
        startActivity(i)
    } else {

    }
}
}

我的 PHP 代码:

<?php
$con=mysqli_connect("localhost","root","","d_database");

if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT id FROM user where 
username='$username' and password='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
    echo $data;
}
mysqli_close($con);
?>

【问题讨论】:

  • 查看How can I prevent SQL-injection in PHP?,您的代码存在漏洞。
  • 只是想知道如何获取数据...没关系,我只是使用它以便于理解...我实际上正在使用 CI(codeigniter)...跨度>
  • 也想知道如何打开一个新的activity并获取和传递数据...

标签: java php android mysql


【解决方案1】:

所以我回答了,我想我没有做足够的研究。

这是我的代码:

public class LoginActivity extends Activity {
Handler h = new Handler();

/*
 * Login
 */



public void DishcoverLogin(View view) {

    EditText usernameField = (EditText)findViewById(R.id.editText_Username_L);
    EditText passwordField = (EditText)findViewById(R.id.editText_Password_L);

    String username = usernameField.getText().toString();
    String password = passwordField.getText().toString();
    new SigninActivity(this).execute(username,password);

}

/*
 * Login
 */

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    //int id = item.getItemId();
    //if (id == R.id.action_settings) {
    //  return true;
    //}
    return super.onOptionsItemSelected(item);
}

private class SigninActivity extends AsyncTask<String,Void,String> {

    private TextView roleField;
    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 password = (String)arg0[1];
            String link="http://192.168.254.108/Login/d_login.php";
            String data  = URLEncoder.encode("username", "UTF-8")
                    + "=" + URLEncoder.encode(username, "UTF-8");
            data += "&" + URLEncoder.encode("password", "UTF-8")
                    + "=" + URLEncoder.encode(password, "UTF-8");
            URL url = new URL(link);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter
                    (conn.getOutputStream());
            wr.write( data );
            wr.flush();
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                sb.append(line);
                break;
            }
            return sb.toString();
        }catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }
    @Override
    protected void onPostExecute(String result){
        String vl=result.toString();

        if(vl.equals("registered")){
            AlertDialog.Builder alt_bld = new AlertDialog.Builder(LoginActivity.this);
            alt_bld.setMessage("D Login Successfull !")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int id) {
                            // Action for 'Yes' Button
                            EditText username = (EditText) findViewById(R.id.editText_Username_L);
                            EditText password = (EditText) findViewById(R.id.editText_Password_L);
                            Intent in = new Intent(getApplicationContext(), DashboardActivity.class);
                            in.putExtra("username", username.toString());
                            in.putExtra("password", password.toString());
                            startActivity(in);
                            finish();
                        }
                    });
            AlertDialog alert = alt_bld.create();
            // Title for AlertDialog
            alert.setTitle("D Login");
            // Icon for AlertDialog
            alert.setIcon(R.drawable.topbar);
            alert.show();
        } else {
            AlertDialog.Builder alt_bld = new AlertDialog.Builder(LoginActivity.this);
            alt_bld.setMessage("D Login Unsuccessfull !")
                    .setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //  Action for 'NO' Button
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = alt_bld.create();
            // Title for AlertDialog
            alert.setTitle("D Login");
            // Icon for AlertDialog
            alert.setIcon(R.drawable.topbar);
            alert.show();
        }
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2015-12-03
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多