【发布时间】:2011-10-09 00:07:45
【问题描述】:
我正在尝试使用来自 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);
}
将我的数据发送到 php 文件的方法 dbConnect 如下所示:
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 方法是错误的。我查看了它,似乎找不到我做错了什么。
您能提供的任何帮助将不胜感激。
路易
【问题讨论】:
-
你还没有说错误是什么或没有发生什么。
-
他说看不到查询是否影响db。
-
对不起,数据库没有更新我通过 onLocationChanged 回调获得的纬度和经度。