I m creating an app using Google Maps Geolocation API that will get the location of user from Network. Google Maps Geolocation API have some attribute to send in url to get the proper output from Google. My Problem is
Cell tower objects - age for reference yu can check here Cell tower objects
WiFi access point objects - age , channel , signalToNoiseRatio for reference you can check here WiFi access point object
i tried TelePhonyManager and Wifi ScaneResult(), but do not able to get these value
if someone know or access Google Maps Geolocation API, Please make suggestion so i will pass these value into Google Map API to get best Result
waiting for response thanks
You can use AsyncTask
to do the network request.
Sample Code:
private class GetGeolocationTask extends AsyncTask<Object, Void, String> {
@Override
protected String doInBackground(Object... arg0) {
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost("https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY");
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("homeMobileCountryCode", 310);
jsonObject.accumulate("homeMobileNetworkCode", 410);
jsonObject.accumulate("radioType", "gsm");
jsonObject.accumulate("carrier", "vodafone");
JSONArray cellTowersArray = new JSONArray();
JSONObject cellTowerObject = new JSONObject();
cellTowerObject.accumulate("cellId", 42);
cellTowerObject.accumulate("locationAreaCode", 415);
cellTowerObject.accumulate("mobileCountryCode", 310);
cellTowerObject.accumulate("mobileNetworkCode", 410);
cellTowerObject.accumulate("age", 0);
cellTowerObject.accumulate("signalStrength", -60);
cellTowerObject.accumulate("timingAdvance", 15);
cellTowersArray.put(cellTowerObject);
jsonObject.accumulate("cellTowers", cellTowersArray);
JSONArray wifiAccessPointsArray = new JSONArray();
JSONObject wifiAccessPointObject = new JSONObject();
wifiAccessPointObject.accumulate("macAddress", "01:23:45:67:89:AB");
wifiAccessPointObject.accumulate("age", 0);
wifiAccessPointObject.accumulate("channel", 11);
wifiAccessPointObject.accumulate("signalToNoiseRatio", 40);
wifiAccessPointsArray.put(wifiAccessPointObject);
jsonObject.accumulate("wifiAccessPoints", wifiAccessPointsArray);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work";
}
catch (MalformedURLException e) {
logException(e);
}
catch (IOException e) {
logException(e);
}
catch (Exception e) {
logException(e);
}
return result;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(YourActivity.this, result, Toast.LENGTH_LONG).show();
}
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
You can call this AsyncTask in your activity like this:
GetGeolocationTask getBlogPostsTask = new GetGeolocationTask();
getGeolocationTask.execute();
You can also read this tutorial for detailed information about how to put JSON in request body.
Or, you can use Google Volley library to do your network request, sample request can be found in this Stackoverflow answer.