【问题标题】:Send data into mySQL using AsyncTask in android在 android 中使用 AsyncTask 将数据发送到 mySQL
【发布时间】:2014-04-23 08:11:31
【问题描述】:

我确实有一个完整的堆,所以我的 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

【问题讨论】:

  • 你能发布你得到的异常吗?此外,如果将 AsyncTask 的输入指定为 ,则不必将 NULL 强制转换为 Void;您可以将大括号留空: new SummaryAsyncTask().execute();
  • 感谢 Smokez!我修复了那个空脚轮..

标签: php android mysql android-asynctask http-post


【解决方案1】:

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'];

【讨论】:

  • 有点丑陋的错误!感谢那!仍然没有写入任何数据,不幸的是,单击注册按钮时根本没有打印出来——顺便说一句:在清单中设置了权限.INTERNET
  • 你能验证mysql_query($string);里面的if($username &amp;&amp; address &amp;&amp; ...正在执行吗?如果是这样,请尝试添加 die,例如:mysql_query($string) or die("error:".mysql_error())
  • hm,1)我没有从我的 php 中得到任何回声,并且 2)在添加 die addtition 后,我得到了上面提到的 gc 错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多