简体   繁体   English

如何将数据从android发送到php

[英]How to send data from android to php

How to send data from android to php ??如何将数据从android发送到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 ??我有类 GPSTracker.java 这个类来获取当前位置,我想将纬度、经度和地址等位置数据发送到服务器,那么,我该怎么办?

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

you should try using an Asynctask from Android side.您应该尝试使用 Android 端的Asynctask your AsyncTask should then be calling a PHP webservice, it'll act like a form has been post to the asked page.然后你的 AsyncTask 应该调用一个 PHP 网络服务,它会像一个表单已经发布到被询问的页面一样。 This can be achieved by using HttpPost request with nameValuePairs (I let you arrange the code as you need).这可以通过使用带有 nameValuePairs 的 HttpPost 请求来实现(我让您根据需要安排代码)。 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. PS:去掉插入代码下的空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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