0

我想通过 Cell Towers 信息获取手机的估计位置。我通过 Android 发布来自 Google Geolocation 的示例请求。我发布请求的代码有什么问题? https://developers.google.com/maps/documentation/business/geolocation/#sample-requests

我使用 Http Post 通过将内容类型设置为 application/json 来发布 JSON 对象。问题是我从调试器获得的请求的结果如下:

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): }

以下是我在 Android 中发布 JSON 请求的方式。

请求的 MainActivity.java 的代码片段。

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

    }
}

我创建了一个类 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;
    }

}
}
4

1 回答 1

0

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

请求正文的 wifiAccessPoints 数组必须包含两个或多个 WiFi 接入点对象。mac地址是必需的;所有其他字段都是可选的。

首先,您至少需要两个 WifiAccessPoints。

第二种解释:

如果响应为 404,则您已确认您的 wifiAccessPoints 和 cellTowers 对象无法进行地理定位。

所有字段都是可选的,因此请尝试使用空的 Wifi 接入点列表发送您的请求,然后使用空的蜂窝塔列表来查看您的问题到底出在哪里。

于 2020-02-04T14:06:19.533 回答