将数据保存到远程数据库

时间:2017-10-23 13:00:02

标签: php android mysql database

我需要存储用户从移动端到远程服务器的信息填充。例如,我在下面写了登录/注册代码。我试过这个,但没有在服务器中获得任何类型的数据存储。我怀疑可能是我的PHP脚本错了。

MainActivity.Java:

public class MainActivity extends Activity {

TextView name, email, number;

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

    name = (TextView) findViewById(R.id.name);
    email = (TextView) findViewById(R.id.email);
    number = (TextView) findViewById(R.id.number);

}

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

public void send(View v) {

    new Send().execute();

}

class Send extends AsyncTask<String, Void, Long> {

    protected Long doInBackground(String... urls) {

        String Name = name.getText().toString();
        String Email = email.getText().toString();
        String Number = number.getText().toString();

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://xxx.000webhostapp.com/phpcode.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("Name", Name));
            nameValuePairs.add(new BasicNameValuePair("Email", Email));
            nameValuePairs.add(new BasicNameValuePair("Number", Number));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
        return null;

    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(Long result) {

        }
    }
}

创建一条新记录来测试它。它到目前为止工作,它创建了新的条目。

php脚本:

<?php

define('hostname', 'localhost');
define('user', 'xxx_username');
define('passwort', 'xxx');
define('databaseName', 'xxx_name');

$conn = mysqli_connect(hostname, user, passwort, databaseName);

if($conn->connect_error){
    die("Connection failed: " .$conn->connect_error);
}


$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Number = $_POST['Number'];

$sql = "INSERT INTO demotable (Name, Email, Number) VALUES ('{$Name}','{$Email}','{$Number}')";

if($conn->query($sql) === TRUE) {
    echo "Record created";
} else {
    echo "Error: ".$sql. $conn->error;
}

$conn->close();

?>

1 个答案:

答案 0 :(得分:0)

您正尝试在 doInBackground()中执行UI操作,因此在进行网络调用之前会导致异常。 UI操作只能在 onPreExecute() onPostExecute()中执行。而网络调用只在 doInBackground()中执行。试试这段代码

class Send extends AsyncTask<String, Void, Long> {
String Name,Email,Number;
@Override
protected void onPreExecute(){
    Name = name.getText().toString();
    Email = email.getText().toString();
    Number = number.getText().toString();
}
@Override
protected Long doInBackground(String... urls) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://affe.000webhostapp.com/phpcode.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("Name", Name));
        nameValuePairs.add(new BasicNameValuePair("Email", Email));
        nameValuePairs.add(new BasicNameValuePair("Number", Integer.toString(Number)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
    return null;

}
@Override
protected void onProgressUpdate(Integer... progress) {

}
@Override
protected void onPostExecute(Long result) {

    }
}