0

I want to get my phone's estimate location through Cell Towers information. I post the sample request from the Google Geolocation through Android. What's wrong in my code to post the request? https://developers.google.com/maps/documentation/business/geolocation/#sample-requests

I use Http Post to post the JSON object by setting the content type as application/json. The problem is the result of the request I obtained from the debugger are as below:

06-03 14:51:06.559: D/response(22250): {
06-03 14:51:06.559: D/response(22250):  "error": {
06-03 14:51:06.559: D/response(22250):   "errors": [
06-03 14:51:06.559: D/response(22250):    {
06-03 14:51:06.559: D/response(22250):     "domain": "geolocation",
06-03 14:51:06.559: D/response(22250):     "reason": "notFound",
06-03 14:51:06.559: D/response(22250):     "message": "Not Found"
06-03 14:51:06.559: D/response(22250):    }
06-03 14:51:06.559: D/response(22250):   ],
06-03 14:51:06.559: D/response(22250):   "code": 404,
06-03 14:51:06.559: D/response(22250):   "message": "Not Found"
06-03 14:51:06.559: D/response(22250):  }
06-03 14:51:06.559: D/response(22250): }

The following is how I post my JSON request in Android.

Code Snippets of MainActivity.java for the request.

public class MainActivity extends Activity {
    private static String url = "https://www.googleapis.com/geolocation/v1/geolocate";
    private JSONParser jsonParser = new JSONParser();
    private JSONObject jsonObject = new JSONObject();
    private JSONObject cellObject = new JSONObject();
    private JSONObject wifiObject = new JSONObject();
    private JSONObject resultObject = new JSONObject();
    private HttpConnection httpConnection = new HttpConnection();
List<NameValuePair> params = new ArrayList<NameValuePair>();

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


  url = url + "?";
  //my_key is replaced by the key obtained from the Google Developer Account.
  //For the API key, I create a new Server Key by putting the IP address as 0.0.0.0/0.



          url = url + "key=" + "my_key";


//the data is the sample request data.
 try {
              cellObject.put("cellId", 42);
              cellObject.put("locationAreaCode",415);
              cellObject.put("mobileCountryCode",310);
              cellObject.put("mobileNetworkCode",410);
              cellObject.put("age", 0);
              cellObject.put("signalStrength", -95);
          }catch (JSONException e)
          {
              e.printStackTrace();
          }

          try{
              wifiObject.put("macAddress", "01:23:45:67:89:AB");
              wifiObject.put("signalStrength", 8);
              wifiObject.put("age", 0);
              wifiObject.put("signalToNoiseRatio", -65);
              wifiObject.put("channel", 8);
          }catch (JSONException e)
          {
              e.printStackTrace();
          }

          try{
              jsonObject.put("homeMobileCountryCode", 310);
              jsonObject.put("homeMobileNetworkCode", 410);
              jsonObject.put("radioType", "gsm");
              jsonObject.put("carrier","T-Mobile");
              jsonObject.put("cellTowers",cellObject); //put nested json object.
              jsonObject.put("wifiAccessPoints", wifiObject);
          }catch (JSONException e)
          {
              e.printStackTrace();
          }

          new PostCell().execute();
    }

    private class PostCell extends AsyncTask<Void,Void,Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //Show progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please Wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {

              resultObject =  httpConnection.sendJSON(jsonObject, url);  

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            //Dismiss dialog
            if(pDialog.isShowing())
                pDialog.dismiss();

    }
}

I have create a class HttpConnection.

public class HttpConnection {

private static final int TIME_OUT = 10000;
private static final int BUF_SIZE = 8;
private static final String CHAR_SET = "iso-8859-1";

public HttpConnection(){}

public JSONObject sendJSON(JSONObject job, String url)
{
    try{
        HttpPost httpPost = new HttpPost(url);
        Log.d("URL: ",url);
        HttpClient client = new DefaultHttpClient();

        //TimeOut Limit
        HttpConnectionParams.setConnectionTimeout(client.getParams(),TIME_OUT);
        StringEntity se = new StringEntity(job.toString());
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        httpPost.setEntity(se);
        HttpResponse response = client.execute(httpPost);
        HttpEntity resEntityGet = response.getEntity();

          if (resEntityGet != null) {
                InputStream inputStream = resEntityGet.getContent();
                BufferedReader reader = new BufferedReader(
                  new InputStreamReader(inputStream, CHAR_SET), BUF_SIZE);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                 sb.append(line + "\n");
                }
                inputStream.close();
                String stringMyJson = sb.toString();

                Log.d("response",stringMyJson);

                JSONObject jsonObject = new JSONObject(stringMyJson);
                return jsonObject;
          }else 
          {
              Log.d("response","null");
              return null;
          }

    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    }catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }catch (JSONException e)
    {
        e.printStackTrace();
        return null;
    }

}
}

1 Answers1

0

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

The request body's wifiAccessPoints array must contain two or more WiFi access point objects. macAddress is required; all other fields are optional.

First of all, you need at least two WifiAccessPoints.

Second explanation :

If the response is a 404, you've confirmed that your wifiAccessPoints and cellTowers objects could not be geolocated.

All fields are optional, so try to send your request with an empty list of Wifi Access Points, and then an empty list of Cell Towers to see where is your problem exactly.

Barbara K
  • 613
  • 6
  • 7