通过 android 使用 php 更新 mysql 数据库
我正在尝试使用 Android 应用程序中的 php 更新 mysql 数据库。我已经成功地能够使用我在这里找到的本教程更新数据库并从数据库返回数据: http://www.helloandroid.com/tutorials/connecting-mysql-database
我现在尝试使用位置提供程序更新数据库,以获取我通过以下方式获取的位置修复的纬度和经度: onLocationChanged 回调。我的代码如下所示:
public void onLocationChanged(Location location) {
double myLonDouble = location.getLongitude();
String myLon = Double.toString(myLonDouble);
double myLatDouble = location.getLatitude();
String myLat = Double.toString(myLatDouble);
sp = getSharedPreferences("MyPrefsFile", 0);
id = sp.getInt("id", 0);
String idString = Integer.toString(id);
DatabaseConnector db = new DatabaseConnector();
ArrayList<NameValuePair> latLon = new ArrayList<NameValuePair>();
latLon.add(new BasicNameValuePair("longitude", myLon));
latLon.add(new BasicNameValuePair("latitude", myLat));
latLon.add(new BasicNameValuePair("id", idString));
tv.setText(idString);
db.dbConnect(latLon, php);
我
dbConnect 方法将我的数据发送到 php 文件,如下所示:
public void dbConnect(ArrayList<NameValuePair> data, String phpFile) {
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(phpFile);
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}// end dbConnect
我在 httppost 对象中使用的 php 文件如下所示:
>><?php
>>$con = mysql_connect("localhost","********","*********");
>>if (!$con)
>> {
>> die('Could not connect: ' . mysql_error());
>> }
>>
>>mysql_select_db("swizzle_practice", $con);
>>
>>$_SQL =
>>"UPDATE users
>>SET longitude = '$_REQUEST[longitude]', latitude = '$_REQUEST[latitude]'
>>WHERE id = '$_REQUEST[id]'";
>>
>>mysql_query($_SQL,$con) or die("error: " . mysql_error());
>>
>>mysql_close($con)
>>?>
的清单如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.first_screen"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".First_screenActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Setup"
android:label="@string/app_name">
</activity>
<activity android:name=".Map"
android:label="@string/app_name">
</activity>
</application>
</manifest>
我没有收到任何错误在日志中这让我认为这是我的代码 dbConnect 方法是错误的。我已经查看过它,似乎找不到我做错了什么。
如果您能提供任何帮助,我们将不胜感激。
路易斯
I am trying to update a mysql database using php from an android application. I have successfully been able to update the database and return data from the database using this tutorial I found here: http://www.helloandroid.com/tutorials/connecting-mysql-database
I am now trying to update a database using a location provider to obtain the latitude and longitude of a location fix that I am getting via the onLocationChanged callback. My code looks like this:
public void onLocationChanged(Location location) {
double myLonDouble = location.getLongitude();
String myLon = Double.toString(myLonDouble);
double myLatDouble = location.getLatitude();
String myLat = Double.toString(myLatDouble);
sp = getSharedPreferences("MyPrefsFile", 0);
id = sp.getInt("id", 0);
String idString = Integer.toString(id);
DatabaseConnector db = new DatabaseConnector();
ArrayList<NameValuePair> latLon = new ArrayList<NameValuePair>();
latLon.add(new BasicNameValuePair("longitude", myLon));
latLon.add(new BasicNameValuePair("latitude", myLat));
latLon.add(new BasicNameValuePair("id", idString));
tv.setText(idString);
db.dbConnect(latLon, php);
}
The method dbConnect, which sends my data to a php file looks like this:
public void dbConnect(ArrayList<NameValuePair> data, String phpFile) {
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(phpFile);
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}// end dbConnect
The php file that I am using in the httppost object looks like this:
>><?php
>>$con = mysql_connect("localhost","********","*********");
>>if (!$con)
>> {
>> die('Could not connect: ' . mysql_error());
>> }
>>
>>mysql_select_db("swizzle_practice", $con);
>>
>>$_SQL =
>>"UPDATE users
>>SET longitude = '$_REQUEST[longitude]', latitude = '$_REQUEST[latitude]'
>>WHERE id = '$_REQUEST[id]'";
>>
>>mysql_query($_SQL,$con) or die("error: " . mysql_error());
>>
>>mysql_close($con)
>>?>
My manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.first_screen"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".First_screenActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Setup"
android:label="@string/app_name">
</activity>
<activity android:name=".Map"
android:label="@string/app_name">
</activity>
</application>
</manifest>
I am not getting any errors in the log which leads me to think that it is my code in the
dbConnect method that is wrong. I have looked over it and can't seem to find what I am doing wrong.
Any help you can give would be much appreciated.
Louis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加
'$_REQUEST['longitude']'
,现在怎么样?记录 nameValuePairs 以检查对值是否正确。PS:我建议使用手动输入的值测试您的 php 代码和查询。
并且您不需要
响应
,因为您在发送键值对后正在执行与您的应用程序无关的操作,即您的查询不会获取任何数据。您可以拥有它,并且可以使用它来输出您的查询是否完成了任务。Add
'$_REQUEST['longitude']'
, how is now? Log the nameValuePairs to check if the pairs values are correct.P.S: I suggest test your php code and your query with manually inputed values.
And you don't need
response
since you are doing operation which has nothing to do with your application after you send the key-values-pairs, i.e your query does not fetch any data. You can have it and you can use it for outputing if your query did the job or not.您可以使用 In your php script 检查脚本收到的数据
。
You can check what data your script received using
In your php script.
好吧,我不太擅长 PHP,但据我所知......您正在从
_REQUEST
访问请求变量,但您正在使用HttpPost
设置请求。简而言之,您正在设置 POST 请求,但在服务器端检索变量作为 GET 请求。要解决此问题,请让客户端发送 GET 请求或从
_POST
在服务器端访问请求变量Well, I'm not very good with PHP, but from what I see..you're accessing request variable from
_REQUEST
, but you're setting the request withHttpPost
.So to put it straight, you're setting a POST request, but retrieving the variable on the server end as a GET request. To fix this, either make the client send a GET request or access request variable on the server side from
_POST