尝试注册用户时,Android应用程序崩溃

时间:2018-11-03 21:26:30

标签: java php android mysql android-studio

我正在尝试将数据用户从应用程序插入MySQL,就像注册用户一样,但是每次崩溃时都会崩溃

我更改了代码,并在Udemy中使用了一位讲师的代码,但仍然崩溃..

我当时以为这是互联网连接问题,但事实并非如此。.互联网工作正常。

我在这里做错了什么?

布局,pRegister

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".pRegister"
    android:background="@color/Background">

    <EditText
        android:id="@+id/pID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="30dp"
        android:ems="10"
        android:inputType="number"
        android:hint="@string/regID"
        android:backgroundTint="@color/white"
        android:textColorHint="@color/white" />

    <EditText
        android:id="@+id/pPhoneNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pID"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:inputType="number"
        android:importantForAutofill="auto"
        android:hint="@string/phoneNo"
        android:backgroundTint="@color/white"
        android:textColorHint="@color/white" />


    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pPhoneNo"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:backgroundTint="@color/white"
        android:textColor="@color/textButton"
        android:textAllCaps="false"
        android:text="@string/regbutton"
        android:onClick="register"
        />
</RelativeLayout>

java活动

    import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class pRegister extends AppCompatActivity {




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

    // register function

    public void register (View view) {

        Intent intent = new Intent(this, Insert.class);
        startActivity(intent);

    }
}

Insert.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;


public class Insert extends AppCompatActivity {

    EditText eID, ePhoneNo;
    String id, phoneNo;

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

        eID = (EditText) findViewById(R.id.pID);
        ePhoneNo = (EditText) findViewById(R.id.pPhoneNo);
    }



    public void register (View view) {
        id = eID.getText().toString();
        phoneNo = ePhoneNo.getText().toString();

        // for java class background task

        String method= "register";
        backgroundTask backgroundTask= new backgroundTask(this);
        backgroundTask.execute(method,id, phoneNo);
        finish();
    }



}

后台Task.java

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

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

    Context ctx;
    backgroundTask(Context ctx){this.ctx=ctx;}


    protected String doInBackground(String... params) {
        // background pRegister
        String reg_url = "/insert.php";
        String method = params[0];
        if(method.equals("register")){
            String id = params[1];
            String phoneNo = params[2];

            try {
                URL url= new URL(reg_url);
                HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os= httpURLConnection.getOutputStream();

                BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));

                String data= URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
                        URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(phoneNo,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream IS= httpURLConnection.getInputStream();
                IS.close();
                return "Registration success";

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

}

insert.php

<?php

require "connect.php";

$id= $_POST["id"];
$phoneNo= $_POST["phoneNo"];
$height= $_POST ["height"];
$weight= $_POST["weight"];
$bloodType= $_POST["bloodType"];
$country= $_POST["country"];
$gender= $_POST["gender"];

$sql = "INSERT into patient (`id`, `phoneNo`) VALUES (`$id`, `$phoneNo`);";



if ($conn->query($sql) === TRUE) {
    echo "data added successfully";
} else {
    echo "Error adding data: " . $conn->error;
}

$conn->close();
?>

0 个答案:

没有答案