douhui1957
douhui1957
2014-04-23 08:11

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

  • http-post
  • android
  • php
  • 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条回答

为你推荐