Hi,
I recieved some questions about the API, so I will post an answer here. Sorry for not providing the full code, but my contract does not allow me to do so
To use the Gears API you need to construct a JSON query and it goes like this:
Using java Syntax Highlighting
JSONObject query = new JSONObject();
try {
query.put("version", "1.1.0");
query.put("host", "maps.google.com");
query.put("home_mobile_country_code", mcc);
query.put("home_mobile_network_code", mnc);
query.put("carrier", tm.getNetworkOperatorName());
query.put("request_address", true);
query.put("address_language", "en_GB");
}
Parsed in 0.031 seconds, using
GeSHi 1.0.8.4
The above works as a header. Then you need to add the cell information. The neighboring cell information you can get from TelephonyManager (getNeighboringCellInfo()). You should add this info to the query:
Using java Syntax Highlighting
for (NeighboringCellInfo cellInfo : cellList) {
JSONObject tower = new JSONObject();
try {
tower.put("cell_id", cellInfo.getCid());
tower.put("location_area_code", cellInfo.getLac());
tower.put("mobile_country_code", mcc);
tower.put("mobile_network_code", mnc);
tower.put("signal_strength", -113 + 2*cellInfo.getRssi());
query.accumulate("cell_towers", tower);
}
}
Parsed in 0.031 seconds, using
GeSHi 1.0.8.4
Then you add the WiFi information from the WifiManager.getScanResults()
Using java Syntax Highlighting
JSONObject wifitower = new JSONObject();
try {
wifitower.put("mac_address", result.BSSID);
wifitower.put("signal_strength", result.level);
query.accumulate("wifi_towers", wifitower);
}
Parsed in 0.033 seconds, using
GeSHi 1.0.8.4
Then you need to post it using httpPost:
Using java Syntax Highlighting
private class Poster {
public String post(JSONObject query) {
Log.d(TAG, "Entering the Poster.post");
// TODO Auto-generated method stub
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> responseHandler = new BasicResponseHandler();
String response = null;
HttpPost postMethod = new HttpPost("https://www.google.com/loc/json");
try {
postMethod.setEntity(new StringEntity(query.toString()));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Exception while creating entity: " + e.getMessage());
}
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");
try {
response = httpClient.execute(postMethod, responseHandler);
Log.d(TAG, "response: " + response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.e(TAG, "ClientProtocol exception while executing: " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "IOException while executing post: " + e.getMessage());
}
return response;
}
}
Parsed in 0.039 seconds, using
GeSHi 1.0.8.4
Then you can read the response using JSONTokener:
Using java Syntax Highlighting
JSONTokener jsontokener= new JSONTokener(response);
jsontokener.skipPast("latitude\":");
String o = jsontokener.nextValue().toString();
String value = new String(o.getBytes(), 0, o.length() -1);
Log.i(TAG, "Next value: " + value);
double lat = Double.parseDouble(value);
Parsed in 0.036 seconds, using
GeSHi 1.0.8.4
and so on. Hope that helps!
Cheers,
Piotr