Android将数据插入到实时mysql数据库中

时间:2017-03-07 11:10:41

标签: php android mysql android-asynctask

我正在尝试将编辑文本中的数据插入到实时数据库中,但是当我按下应发送数据的按钮时应用程序崩溃。

  public class MainActivity extends Activity {
    Button add_btn,view_btn;
    TextView txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    add_btn=(Button)findViewById(R.id.add_btn);
    view_btn=(Button)findViewById(R.id.view_btn);
    txt=(TextView)findViewById(R.id.textView1);
    ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
    if(networkInfo!=null && networkInfo.isConnected()){
        txt.setVisibility(View.INVISIBLE);
    }
    else
    {   
        add_btn.setEnabled(false);
        view_btn.setEnabled(false);
        txt.setVisibility(View.VISIBLE);
    }
}
public void add_user(View view){

    startActivity(new Intent(this,Add_user.class));

}
public void view_user(View view){


}

        }


       public class Add_user extends Activity {
    EditText name,username,pass;
    Button add_user;
   String name1,username1,pass1,method;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_user);
    name=(EditText)findViewById(R.id.username);
    username=(EditText)findViewById(R.id.name);
    pass=(EditText)findViewById(R.id.pass);
    add_user=(Button)findViewById(R.id.button1);
}
public void add(View view){
    name1=name.getText().toString();
    username1=username.getText().toString();
    pass1=pass.getText().toString();
    BackgroundTask backgroundTask=new BackgroundTask();
    backgroundTask.execute(name1,username1,pass1);
    finish();
}
class BackgroundTask extends AsyncTask<String, Void, String> {
    String url;
    @Override
    protected void onPreExecute() {
        url="http://douxtoile.com/add_user.php";
        Toast.makeText(getApplicationContext(), url,Toast.LENGTH_LONG).show();

    }
    @Override
    protected String doInBackground(String... params) {
        String name=params[1];
        String username=params[2];
        String pass=params[3];
        Toast.makeText(getApplicationContext(), name+""+username+""+pass,Toast.LENGTH_LONG).show();
        try {

                URL url1=new URL(url);
                HttpsURLConnection httpsURLConnection=(HttpsURLConnection)url1.openConnection();
                httpsURLConnection.setRequestMethod("POST");
                httpsURLConnection.setDoOutput(true);
                OutputStream os=httpsURLConnection.getOutputStream();
                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                String data=URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
                        URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
                        URLEncoder.encode("pass","UTF-8")+"="+URLEncoder.encode(pass,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            os.close();
            InputStream is=httpsURLConnection.getInputStream();
            is.close();
            httpsURLConnection.disconnect();

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        catch(NullPointerException e){
            Toast.makeText(getApplicationContext(), e.toString(), 0).show();

        }
        catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

        }
        return "Success";
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
    }


}
 }

这是我的错误日志。

Stacktrace: 
          03-07 16:05:34.324: E/AndroidRuntime(24090): FATAL EXCEPTION:        AsyncTask #2
          03-07 16:05:34.324: E/AndroidRuntime(24090): Process:     com.example.demo_online_db, PID: 24090
          03-07 16:05:34.324: E/AndroidRuntime(24090):   java.lang.RuntimeException: An error occured while executing doInBackground()
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at android.os.AsyncTask$3.done(AsyncTask.java:300) 
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at java.lang.Thread.run(Thread.java:818)
          03-07 16:05:34.324: E/AndroidRuntime(24090): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at com.example.demo_online_db.Add_user$BackgroundTask.doInBackground(Add_user.java:54)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at com.example.demo_online_db.Add_user$BackgroundTask.doInBackground(Add_user.java:1)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
          03-07 16:05:34.324: E/AndroidRuntime(24090):  ... 4 more
          03-07 16:05:35.985: E/OpenGLRenderer(24090): SFEffectCache:clear(), mSize = 0

我没有包括任何外部罐子,实际上我是新手,因此我不确切地知道我这样做是对的还是错的。 PHP脚本很好我已经静态测试过了。

2 个答案:

答案 0 :(得分:1)

以这种方式获取字符串值...

        String name=params[0];
        String username=params[1];
        String pass=params[2];

答案 1 :(得分:0)

在此处进行3次更改

1)

public void add(View view){
    name1=name.getText().toString();
    username1=username.getText().toString();
    pass1=pass.getText().toString();
    BackgroundTask backgroundTask=new BackgroundTask();
    backgroundTask.execute(name1,username1,pass1);
    //finish();
}

2)

@Override
protected String doInBackground(String... params) {
    String name=params[0];
    String username=params[1];
    String pass=params[2];

3)

@Override
    protected void onPostExecute(String result) {

        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        finish();
    }