用于连接php mySQL数据库的Android代码


Android code for connecting with php mySQL database

我正在通过php研究mySQL和android连接当我编写以下代码时,我的应用程序突然崩溃:

package com.googlecode.javacv.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener {
    EditText etUsername, etPassword, etname;
    Button btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etUsername = (EditText)findViewById(R.id.etUsername);
        etPassword = (EditText)findViewById(R.id.etPassword);
        etname = (EditText)findViewById(R.id.etname);
        btnLogin = (Button)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(this);
    }
    @Override
    public void processFinish(String output) {
        Toast.makeText(this, output, Toast.LENGTH_LONG).show();
    }
    @Override
    public void onClick(View v)
    {
        HashMap postData= new HashMap();
        postData.put("txtUsername",etUsername.getText().toString());
        postData.put("txtPassword", etPassword.getText().toString());
        postData.put("name", etname.getText().toString());
        PostResponseAsyncTask task = new PostResponseAsyncTask (MainActivity.this , (AsyncResponse) postData);
        task.execute("http://theeatery.org/uet1.php");
    }
}

有什么建议为什么会这样吗?请帮助我找出并解决问题...提前致谢

您正在使用我的 https://github.com/kosalgeek/generic_asynctask_v2 库。这个新版本构造函数需要 3 个参数:1) 上下文,2) HashMap 和 3) AsyncReponse。

你要做的是:

1) 从:

PostResponseAsyncTask task = new PostResponseAsyncTask (MainActivity.this , (AsyncResponse) postData);

自:

PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, this);

2)或者最好将它与匿名类一起使用来处理AsyncResponse,如下所示:

PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {
  @Override
  public void processFinish(String s) {
    Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
  }
});