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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)