【问题标题】:Posting on server from android phone从安卓手机在服务器上发帖
【发布时间】:2012-04-30 19:06:54
【问题描述】:

已经很久了,经过大量研究后,我无法找到我真正想要的解决方案。我制作了一个 GPS 跟踪应用程序。现在我想将我从手机获得的坐标发布到我的本地主机服务器。而且它也每 5 分钟发布一次坐标。我使用 WAMP 制作了一个服务器。

PHP 脚本

?php

echo 'Hello, world!';

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ric_db", $con);

$devid = $_POST["devid"];
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];





$sql = "INSERT INTO  `ric_db`.`locations` (
`id` ,
`devid` ,
`latitude` ,
`longitude` ,
`service`
)
VALUES (
NULL ,  '$devid',  '$latitude',  '$longitude',  '$service'
);";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

mysql_close($con);

?>

Java 文件

@Override 

public void onCreate(Bundle savedInstanceState) 

{ 

super.onCreate(savedInstanceState); 

setContentView(R.layout.main); 



/* Use the LocationManager class to obtain GPS locations */ 

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

LocationListener mlocListener = new MyLocationListener(); 



mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 



} 



/* Class My Location Listener */ 

public class MyLocationListener implements LocationListener 

{ 
 Context context;
public void onLocationChanged(Location loc) 

{ 
StringBuilder sb = null;
   String result = null;
   InputStream is = null;

double latitude = loc.getLatitude(); 

    double longitude = loc.getLongitude(); 
   Toast.makeText(context, "Latitude:" +latitude + "Longitude:" +longitude, 5000).show();
  TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
     String Devid = telephonyManager.getDeviceId();




//this is JSON part to put your information inside it
String postData = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"Devid\":\""+Devid+"\",\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\"}}";

String url="http://localhost/mehul/store.php";


List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Devid", Devid));
nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(latitude) )); 
nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(longitude))); 

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

HttpEntity http_entity = response.getEntity();

Log.i("postData", response.getStatusLine().toString());
BufferedReader br = new BufferedReader(
new InputStreamReader(http_entity.getContent()));
String res = br.readLine();
System.out.println(res);


}





public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

请帮我解决这个问题...

【问题讨论】:

  • 请提供更多细节。我可以看出您正在尝试将数据放入服务器上的 mysql 数据库中,但您没有提到您遇到了什么错误或遇到了什么问题。
  • 另外,您是使用数据交换格式 (json/xml) 还是只是将这些作为嵌入在请求中的 post/get 字段发送?
  • 您在帖子中没有提出任何问题。请提出具体的编程问题。
  • 实际上我是 json 新手,所以不完全知道该怎么做.. 有人可以帮助我吗.. gps 应用程序运行良好,只想发布它.. 我有什么变化在 mt php 中制作
  • 您不需要在 MySQL 查询中再次指定数据库的名称。只需表名即可。而$service 似乎没有设置。此外,您的代码容易受到 SQL 注入的攻击,因此请使用 mysql_real_escape_string 转义您的用户输入。

标签: php android latitude-longitude


【解决方案1】:

不要在你的 url 中使用localhost,而是使用服务器的 IP 地址。 而且您的 PHP 脚本有错误的开始标记:?php 而不是 &lt;?php

如果这没有帮助,请评论您会遇到什么错误...

【讨论】:

  • 如何获取服务器的ip地址...?
  • 在 Windows 中:打开命令提示符 (run&gt;cmd) 并输入 ipconfig,您将获得您的 IP 地址。在 Ubuntu 中:右键单击网络小程序并选择 connection information
猜你喜欢
  • 2011-11-16
  • 1970-01-01
  • 2012-07-21
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 2016-08-23
  • 1970-01-01
相关资源
最近更新 更多