php - 在 android 中使用 AsyncTask 将数据发送到 mySQL

标签 php android mysql android-asynctask http-post

我确实有一个完整的堆,所以我的 gc 告诉我什么都不会发送 “GC_CONCURRENT 释放 277K,10% 释放 8120K/8967K,暂停 13ms+5ms,总计 47ms

我想使用 php、http-Post 和 AsyncTask 将一些数据从 EditText 发送到 mySQL但找不到错误。

插入.php

            $link = mysql_connect("www.host.de", "user" , "pw");  
            if (!$link) {
            die('Verbindung nicht möglich : ' . mysql_error());
        } else{
            echo "Connected to MySQL<br>";
        }

        // benutze Datenbank db
        $db_selected = mysql_select_db('db', $link);
        if (!$db_selected) {
            die ('Kann db nicht benutzen : ' . mysql_error());
        }

            $id="";
            $userid="";

                $id = "SELECT MAX(userid) FROM db.user";
                $my_id = mysql_query($id);

                if (!$my_id) {
            echo 'Anfrage ($my_id) konnte nicht ausgeführt werden : ' . mysql_error();
            exit;
        }  
            mysql_free_result($my_id);

                    while($object = mysql_fetch_assoc($my_id)){  
                        $output[] = $object;  
        }               
                        $userid=$output[0];
                        echo "maxId: " . $userid;       

            $username = $_REQUEST['reg_fullname'];  
            $address = $_REQUEST['reg_address'];
            $postcode = $_REQUEST['reg_postcode'];
            $town = $_REQUEST['reg_town'];
            $country = $_REQUEST['reg_country'];
            $password = $_REQUEST['reg_password'];
            $motherbornin = $_REQUEST['reg_motherbornin'];
            $mail = $_REQUEST['reg_mail'];


                if($username && $pasword && $address && $postcode && $town && $country && $motherbornin && $mail){  
                    $string = ("INSERT INTO db.user(userid, name, address, postcode, town, country, password, motherbornin, mail) VALUES ('$userid[0]', '$username', '$address', '$postcode', '$town', '$country', '$password', '$motherbornin', '$mail')");  
                    mysql_query($string);  
                    echo $string;
                }  

                $string = "SELECT name FROM db.user";  
                $my_string = mysql_query($string);  


                  //  while($object = mysql_fetch_assoc($my_string)){  
                    //    $output[] = $object;  


                   // echo json_encode($output);  
          //      }
            mysql_close($link);  
        ?>

RegisterUserActivity.java

    import java.util.ArrayList;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;

    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    import com.example.books4locals.R;

    public class RegisterUserActivity extends Activity {

        EditText registerMail, registerPassword, registerName, registerFirstName,
                registerAddress, registerTown, registerPostCode, registerCountry, registerMotherBornIn;
        Button button;

        String mailValue, passwordValue, nameValue, addressValue, townValue, postcodeValue, countryValue, motherborninValue;

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.registeruseractivity_main);

            registerName = (EditText) findViewById(R.id.reg_fullname);
            registerAddress = (EditText) findViewById(R.id.reg_address);
            registerPostCode = (EditText) findViewById(R.id.reg_postcode);
            registerTown = (EditText) findViewById(R.id.reg_town);
            registerCountry = (EditText) findViewById(R.id.reg_country);
            registerPassword = (EditText) findViewById(R.id.reg_password);
            registerMotherBornIn = (EditText) findViewById(R.id.reg_motherbornin);
            registerMail = (EditText) findViewById(R.id.reg_mail);

            button = (Button) findViewById(R.id.btnRegister);

             button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        nameValue = registerName.getText().toString();
                        addressValue = registerAddress.getText().toString();
                        postcodeValue = registerPostCode.getText().toString();
                        townValue = registerTown.getText().toString();
                        countryValue = registerCountry.getText().toString();
                        passwordValue = registerPassword.getText().toString();
                        motherborninValue = registerMotherBornIn.getText().toString();
                        mailValue = registerMail.getText().toString();
                        new SummaryAsyncTask().execute((Void) null);
                    }
                }); 
        }

        class SummaryAsyncTask extends AsyncTask<Void, Void, Boolean> {

            private void postData(String name, String address, String postcode,
                    String town, String country, String password, String motherbornin, String mail) {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.host.de/insert.php");

                try {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(8);
                    nameValuePairs.add(new BasicNameValuePair("name", name));
                    nameValuePairs.add(new BasicNameValuePair("address", address));
                    nameValuePairs.add(new BasicNameValuePair("postcode", postcode));
                    nameValuePairs.add(new BasicNameValuePair("town", town));
                    nameValuePairs.add(new BasicNameValuePair("country", country));
                    nameValuePairs.add(new BasicNameValuePair("password", password));
                    nameValuePairs.add(new BasicNameValuePair("motherbornin", motherbornin));
                    nameValuePairs.add(new BasicNameValuePair("mail", mail));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                }
                catch(Exception e)
                {
                    Log.e("log_tag", "Error:  "+e.toString());
                }
            }

            @Override
            protected Boolean doInBackground(Void... params) {
                postData(nameValue, addressValue, postcodeValue, townValue, countryValue, passwordValue, motherborninValue, mailValue);
                return null;
            }
        }
    }

提前致谢

按照 Log1c 的步骤更新错误:

http://s23.postimg.org/441uk15qj/Screen_Shot_2014_04_23_at_11_48_26.png

最佳答案

PHP 中的参数名称应与您从 android 传递的参数名称相匹配。

insert.php 中更改它

        $username = $_REQUEST['reg_fullname'];  
        $address = $_REQUEST['reg_address'];
        $postcode = $_REQUEST['reg_postcode'];
        $town = $_REQUEST['reg_town'];
        $country = $_REQUEST['reg_country'];
        $password = $_REQUEST['reg_password'];
        $motherbornin = $_REQUEST['reg_motherbornin'];
        $mail = $_REQUEST['reg_mail'];

        $username = $_REQUEST['name'];  
        $address = $_REQUEST['address'];
        $postcode = $_REQUEST['postcode'];
        $town = $_REQUEST['town'];
        $country = $_REQUEST['country'];
        $password = $_REQUEST['password'];
        $motherbornin = $_REQUEST['motherbornin'];
        $mail = $_REQUEST['mail'];

关于php - 在 android 中使用 AsyncTask 将数据发送到 mySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23238510/

相关文章:

使用两个 chrome 浏览器时,php 应用程序 session 会混淆

php - php文件中的HTML按钮

javascript - 如何通过单击其中的按钮来删除父 iframe?

android - 将 pjsip 与安卓一起使用

android - React Native - 适用于 Shadow 的 Android native 组件

MySQL 连接返回 Null

mysql - 如何解决致命的 SQL 语句错误?

javascript - 我可以对 PHP 数组进行 json_encode 并将值作为 JS RegExp 对象获取吗?

mysql - 当 log_bin 关闭时删除 log-bin 文件

android - Android中EEG信号的FFT理解代码