I am sending android user location lat and longitude to server together with registration ID
and other parameters as json
object in Asynctask(a nested class of the activity). But the Location object(that had values at the start of the app) instantiated in the activity as such
location = LocationServices.FusedLocationApi
.getLastLocation(mgoogleapiclient);
is returning null in the Asynctask Class. Can someone explain to me why? Do I have to use separate class to get user location and send it in another asynctask or service(which doesn't make sense from architectural standpoint)?
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
PostData pdata = new PostData();
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
pdata.execute(String.valueOf(latitude), String.valueOf(longitude));
}
I am retrieving this from the AsyncTask class as such:
json.put("latitude", String.valueOf(args[1]));
json.put("longitude", String.valueOf(args[2]));
But when I debug it, I am getting the getting the registration ID which I sent to the AsyncTask class from another method. I hope I am making myself clear.
@Override
protected String doInBackground(String... args) {
try {
try {
URL url;
HttpURLConnection urlConn;
url = new URL ("myphp.php");
urlConn = (HttpURLConnection)url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");
urlConn.setRequestProperty("Accept", "application/json");
urlConn.setChunkedStreamingMode(0);
urlConn.setRequestMethod("POST");
urlConn.connect();
//get google account
AccountManager am = AccountManager.get(getBaseContext()); // "this" references the current Context
Account[] accounts = am.getAccountsByType("com.google");
//Create JSONObject here
JSONObject json = new JSONObject();
json.put("regID", String.valueOf(args[0]));
json.put("Google account", accounts[0].name);
json.put("name", "Amanda");
json.put("tel", "2069994444");
json.put("latitude", String.valueOf(args[1]));
json.put("longitude", String.valueOf(args[2]));
String postData=json.toString();
// Send POST output.
OutputStreamWriter os = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
os.write(postData);
Log.i("NOTIFICATION", "Data Sent");
os.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String msg="";
String line = "";
while ((line = reader.readLine()) != null) {
msg += line; }
Log.i("msg=",""+msg);
} catch (MalformedURLException muex) {
// TODO Auto-generated catch block
muex.printStackTrace();
} catch (IOException ioex){
ioex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ERROR", "There is error in this code");
}
return null;
}
The following is how I sent the registration ID
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (!regid.isEmpty()) {
PostData pd = new PostData();
pd.execute(regid);
} else {
//register
registerInBackground();
}