【发布时间】:2015-07-04 17:18:40
【问题描述】:
我的应用程序是一个简单的跟踪系统,其中每个用户的位置都会更新到服务器上,当需要帮助时,它会发回给定范围(大约)内所有人的手机号码。 该应用程序可以完美地定期将位置更新到服务器,并使用一种近似方法来查找一定范围内的人我现在面临的问题是将查询的号码列表发送回设备。我已经尝试了一些教程,但没有运气,应用程序不断崩溃。 这是我的代码,我将数据与 php 代码一起发送。如果有人帮我弄清楚如何从服务器接收回号码。
private void serverConnection() {
// TODO Auto-generated method stub
GPSTracker gps1 = new GPSTracker(MainActivity.this);
double latitude = gps1.getLatitude();
double longitude = gps1.getLongtitude();
Toast.makeText(getApplicationContext(),"Your Location is -\nLat:"+latitude+"\nLon:"+longitude,Toast.LENGTH_LONG).show();
String lat = String.valueOf(latitude);
String log = String.valueOf(longitude);
String svdNum;
RegistrationActivity gsm = new RegistrationActivity();
SharedPreferences sharedData = getSharedPreferences(gsm.filename,0);
svdNum = sharedData.getString("Mobile Number", "No Number Registered");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Lat",lat));
nameValuePairs.add(new BasicNameValuePair("Long",log));
nameValuePairs.add(new BasicNameValuePair("Number",svdNum));
try{
if(distress == 0){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/tech/serverConnection.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}else{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/tech/serverConnection2.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
}
catch(ClientProtocolException e){
Log.e("ClientProtocol", "Log_tag");
e.printStackTrace();
}
catch(IOException e){
Log.e("Log_tag", "IOException");
e.printStackTrace();
}
php代码
<?php
$conn = mysql_connect(localhost, 'root', '');
mysql_select_db('finalyearproject');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$Number=$_POST['Number'];
$LatitudeS=$_POST['Lat'];
$LongitudeS=$_POST['Long'];
$Latitude = floatval($LatitudeS);
$Longitude = floatval($LongitudeS);
$sql = "INSERT INTO app_backup(Number,Latitude, Longitude) VALUES('$Number', '$Latitude', '$Longitude') ON DUPLICATE KEY UPDATE Latitude=VALUES(Latitude), Longitude=VALUES(Longitude)";
$sql = "UPDATE app_backup SET Lat_diff=Latitude-$Latitude ,Long_diff=Longitude-$Longitude";
$query= mysql_query("SELECT Number FROM app_backup WHERE Lat_diff <= 30 AND Long_diff <=30 AND Long_diff <> 0 AND Lat_diff <> 0 AND Long_diff >= -30 AND Lat_diff >= -30");
$column = array();
while ( $row = mysql_fetch_array($query, MYSQL_ASSOC) ) {
$column[] = $row['Number'];
}
echo json_encode(column);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
在此先感谢您。
编辑: 应用程序不断崩溃的代码块
HttpEntity entity2 = response.getEntity();
InputStream nums = entity2.getContent();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(nums,"iso-8859-1") );
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
nums.close();
res = sb.toString();
}
catch(IOException e){
Log.e("Log_tag", "IOException");
e.printStackTrace();
}
Json 循环
try{
JSONArray jarray = new JSONArray(res);
JSONObject json_data = null;
for(int i=0; i < jarray.length(); i++)
{
json_data = jarray.getJSONObject(i);
}}
catch(JSONException e){
Log.e("error parsing data", "Log_tag");
e.printStackTrace();}
添加这两个块后,logcat 说“关闭 VM”,应用程序崩溃了。我不太确定我的方法在检索方面是否正确。
【问题讨论】:
-
您说“应用不断崩溃”,但没有说在哪里或如何崩溃。你能详细说明一下吗?
-
我已经编辑了有关应用程序崩溃的问题。 @WaiHaLee