簡體   English   中英

如何將數據從android發送到php

[英]How to send data from android to php

如何將數據從android發送到php?

我有類 GPSTracker.java 這個類來獲取當前位置,我想將緯度、經度和地址等位置數據發送到服務器,那么,我該怎么辦?

和這個 GPSTracker.java http://pastebin.com/0ZVUdC0w

您應該嘗試使用 Android 端的Asynctask 然后你的 AsyncTask 應該調用一個 PHP 網絡服務,它會像一個表單已經發布到被詢問的頁面一樣。 這可以通過使用帶有 nameValuePairs 的 HttpPost 請求來實現(我讓您根據需要安排代碼)。 然后你可以用服務器端的值做任何你想做的事情

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);
        }

您可能還需要以下權限

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

希望能幫助到你

// 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:去掉插入代碼下的空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM