简体   繁体   中英

How to send data from android to php

How to send data from android to php ??

i have class GPSTracker.java this class to get current location and i want to send location data like latitude , longitude and adress to server, So, what should I do ??

and this GPSTracker.java http://pastebin.com/0ZVUdC0w

you should try using an Asynctask from Android side. your AsyncTask should then be calling a PHP webservice, it'll act like a form has been post to the asked page. This can be achieved by using HttpPost request with nameValuePairs (I let you arrange the code as you need). And then you can do whatever you want with the value on the server side

HttpClient client = new DefaultHttpClient();

        // Http Request Params Object
        HttpPost post = new HttpPost("SERVER_ADRESS/Webservice.php");

        // Any other parameters you would like to set
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("Key", Value));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);

        post.setEntity(entity);

        // Response from the Http Request
        response = client.execute(post);

        StatusLine statusLine = response.getStatusLine();

        // Check the Http Request for success
        if (statusLine.getStatusCode() == HttpStatus.SC_OK)
        {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();

            // Put value returned by the Webservice in content
            content = out.toString();

            doSomethingWithReturnedValue(content);
        }

You might also need the following permission

<uses-permission android:name="android.permission.INTERNET" />

Hope it helps

// Include config file
include_once('confi.php');

if($_SERVER['REQUEST_METHOD'] == "POST"){
 // Get data


 $name = isset($_POST['name']) ? mysql_real_escape_string($_POST['name']) : "";

 $email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : "";

 $password = isset($_POST['pwd']) ? mysql_real_escape_string($_POST['pwd']) : "";

 $status = isset($_POST['status']) ? mysql_real_escape_string($_POST['status']) : "";

 // Insert data into data base
 $sql = "INSERT INTO `    kwd   `.`   users  ` (`   I D   `, `   n a m e   `, `   e m a i l   `, `   p a s s word  `, `s t a t us`) VALUES (NULL, '$name', '$email', '$password', '$status');";
 $qur = mysql_query($sql);
 if($qur){
 $json = array("status" => 1, "msg" => "Done User added!");
 }else{
 $json = array("status" => 0, "msg" => "Error adding user!");
 }
}else{
 $json = array("status" => 0, "msg" => "Request method not accepted");
}

@mysql_close($conn);

/* Output header */
 header('Content-type: application/json');
 echo json_encode($json);

PS: remove the space under the insert codes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM