douhui1957 2014-04-23 08:11
浏览 97
已采纳

使用Android中的AsyncTask将数据发送到mySQL

I do have a full heap, so my gc said me and nothing is going to be send "GC_CONCURRENT freed 277K, 10% free 8120K/8967K, paused 13ms+5ms, total 47ms"

I want to send some data from EditText to a mySQL using php, http-Post and AsyncTask but can't find the error.

insert.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;
            }
        }
    }

Thanks in advance

UPDATE ERROR after following the steps of Log1c:

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

  • 写回答

1条回答 默认 最新

  • dtrz99313 2014-04-23 08:19
    关注

    Your parameter name in PHP should match with the one you are passing from android.

    change this in 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'];
    

    TO

            $username = $_REQUEST['name'];  
            $address = $_REQUEST['address'];
            $postcode = $_REQUEST['postcode'];
            $town = $_REQUEST['town'];
            $country = $_REQUEST['country'];
            $password = $_REQUEST['password'];
            $motherbornin = $_REQUEST['motherbornin'];
            $mail = $_REQUEST['mail'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组