Using the Google Maps Geolocation API

I use the following code to get lat. a long. by providing MCC, MNC I use the Google Maps geolocation API for this, but I get the same results (lat / long) for different MCC / MNC values. Even when I request empty json, I get the same results (lat / long). Where am I mistaken?

public class CellID { public static void main(String[] args) { try{ putDataToServer("https://www.googleapis.com/geolocation/v1/geolocate?key=mykey",null); } catch(Throwable throwable){ System.out.println("Error"); } } public static String putDataToServer(String url,JSONObject returnedJObject) throws Throwable { HttpPost request = new HttpPost(url); JSONStringer json = (JSONStringer) new JSONStringer() .object() .key("mobileCountryCode").value(504) .key("mobileNetworkCode").value(0) .key("locationAreaCode").value(0) .key("cellID").value(0) .endObject(); System.out.println("json"+json.toString()); StringEntity entity = new StringEntity(json.toString(), "UTF-8"); request.setEntity(entity); HttpResponse response =null; HttpClient httpClient = new DefaultHttpClient(); try{ response = httpClient.execute(request); } catch(SocketException se) { throw se; } BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); //Displaying the response received. String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); if (line.startsWith("Auth=")) { String key = line.substring(5); // Do something with the key } } return response.getEntity().toString(); } } 
+4
source share
2 answers

Is your JSON request object complete? I mean, it looks like the keys you are using are part of one description of the "tower", but this is only part of the large request body, which should be formatted as follows:

 { "homeMobileCountryCode": 310, "homeMobileNetworkCode": 410, "radioType": "gsm", "carrier": "Vodafone", "cellTowers": [ // See the Cell Tower Objects section below. ], "wifiAccessPoints": [ // See the WiFi Access Point Objects section below. ] } 

If tower features are formatted as follows:

 {'cellTowers': [ { 'cellId': 42, 'locationAreaCode': 415, 'mobileCountryCode': 310, 'mobileNetworkCode': 410, 'age': 0, 'signalStrength': -60, 'timingAdvance': 15 } ]} 

I think I am missing how your json object becomes full?

https://developers.google.com/maps/documentation/business/geolocation/

+1
source

It seems that the problem is that the HttpPost object by default sends its parameters as x-www-form-urlencoded , but you need to send it as application/json . This section explains what happens if you do this: How to use parameters with HttpPost

There are several ways that should fix this. One of them is to set the Content-type header on the HttpPost object:

 request.setHeader("Content-type", "application/json"); 

Another, which I think is better, is to use the setContentType method for StringEntity registered here

 entity.setContentType("application/json"); 

Any of these single lines used before sending the request should fix the problem.

+3
source

Source: https://habr.com/ru/post/1491856/


All Articles