Frage

I am Using following code to get the lat. long. by providing MCC, MNC I am using Google Maps Geo-location API for this but I am getting same results (lat/long) for different MCC/MNC values . Even when I am requesting blank json I am getting same results(lat/long). Where am I going wrong ?

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();

    }

}
War es hilfreich?

Lösung 2

Is your JSON request object complete? I mean it looks like the keys you are using are part of a single "tower" description, but that this is just part of the larger request body, which should be formatted like:

{
  "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.
  ]
}

Where the tower objects are formatted like:

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

I guess I'm missing how your json object gets turned into the complete one?

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

Andere Tipps

It looks like the problem is here is that the HttpPost object defaults to sending its parameters as x-www-form-urlencoded, but you need to send it as application/json. This thread explains what happens if you do this: How to use parameters with HttpPost

There are a couple of ways that should fix it. One is to set the Content-type header on the HttpPost object:

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

The other, which I think is better, is to use the setContentType method of StringEntity documented here

entity.setContentType("application/json");

Either of these single lines used before you send the request should fix the problem.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top