从Android的数据发送到PHP [英] Send data from android to php

查看:158
本文介绍了从Android的数据发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助有关从Android发送数据到PHP。我有这个Java code为Android;

I need to help about to send data from android to php. I have this java code for android;

public void sendToDb()
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("pLat",Double.toString(lat)));
    nameValuePairs.add(new BasicNameValuePair("pLng",Double.toString(lng)));

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new      
                HttpPost("http://123456.com/welcome.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        Log.i("postData", response.getStatusLine().toString());
    }

    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection "+e.toString());
    }           
}

和我在我的PHP文件夹此codeS;

and I have in my php folder this codes;

<?php
 $pLat = $_POST['pLat'];
 $pLng = $_POST['pLng'];

 $result = mysql_query("update Users set lat='$pLat',lon='$pLng' where username='ozi');      
 if (mysql_affected_rows()==0) 
   {
    $query = "INSERT INTO Users ";
    $query .= "(lat,lon) VALUES ('$pLat','$pLng')";
   }
 }
?>

当我把数据从我的手机是没有问题的,但在我看的MySQL lat和大量的行不为空,但空。
是否有您在$ C $看到一个问题CS?

When I send to data from my phone there is no problem but after I look mysql lat and lot rows not null but empty. Is there a problem that you see in codes?

推荐答案

读你的意见,你正在试图做的(我认为)是什么后保持数据。 PHP文件不做任何事,但运行code你有。如果您在请求通过他们的POST变量是唯一活着。该请求是从Android应用来跟POST变量一起和Android应用程序的响应将有正确的输出。

After reading your comments, what you're trying to do (I think) is persist data. The php file does nothing but run the code you have. The POST variables are only alive if you pass them in the request. The request is coming from the Android app along with POST variables and the response to the Android app will have the correct output.

从浏览器传来的请求是GET,不发送或开发平台PLNG变量,所以没有变量输出。

The request coming from your browser is a "GET" and does not send a pLat or pLng variable, so there is no variables to output.

这是HTTP,你必须掌握的一个很基本的概念。

This is a very basic concept of HTTP that you must grasp.

你所试图做的是保存数据的地方。 PHP可以通过它保存到一个数据库(如MySQL),或简单地输出POST变量到本地文件做到这一点。快速谷歌搜索可以告诉你如何保存文件和打开数据库。哎呀,你甚至可以只给他们在一封电子邮件给自己。

What you are trying to do is save the data somewhere. PHP can do that by saving it to a database (such as MySQL) or by simply outputting the POST variables to a local file. A quick google search can show you how to save files and open databases. Heck, you can even just send them in an email to yourself.

在PHP脚本只能保持剧本的生活这些变量,然后释放出来的内存永远。这取决于你使用PHP的力量来拯救那些变量的其他地方。

The PHP script will only hold those variables for the life of the script and then release them out of memory forever. It's up to you to use the power of PHP to save those variables somewhere else.

这篇关于从Android的数据发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆