My app is a simple tacking system where the every users location is updated onto the server and when called for help it is to send back Mobile numbers of all the people within a given range(approx). The app works perfectly updating the location periodically to the server and uses an approximate method to find people within a certain range the problem im facing now is to send the queried list of numbers back to the device. I have tried a few tutorials but with no luck the app keeps crashing. Here is my code where i send data along with the php code. If anyone to help me figure out how to receive the numbers back from the server.
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();
}
The php code
<?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);
?>
Thank you in Advance.
Edit: The block of code after which the app kept crashing
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 loop
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();}
after adding these two blocks the logcat said 'shutting down VM' and the app crashed. Im not quite sure if my approach is correct on the retrieving side.