我正在使用Google Maps Geolocation API创建一个应用,该应用将从网络获取用户的位置。 Google Maps Geolocation API具有一些属性,可以发送网址以获取Google的正确输出。 我的问题是

  1. 蜂窝塔对象-参考年龄可以在这里检查蜂窝塔对象

  2. WiFi接入点对象-age,channel,signalToNoiseRatio供参考,您可以在此处检查WiFi接入点对象

我尝试了TelePhonyManager和Wifi ScaneResult(),但无法获得这些值

如果有人知道或访问Google Maps Geolocation API,请提出建议,以便我将这些值传递给Google Map API以获取最佳结果

等待回应谢谢

===============>>#1 票数:1 已采纳

您可以使用AsyncTask进行网络请求。

样例代码:

  private class GetGeolocationTask extends AsyncTask<Object, Void, String> {

        @Override
        protected String doInBackground(Object... arg0) {
            InputStream inputStream = null;
            String result = "";

            try {
                // 1. create HttpClient
                HttpClient httpclient = new DefaultHttpClient();

                // 2. make POST request to the given URL
                HttpPost httpPost = new HttpPost("https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY");

                String json = "";

                // 3. build jsonObject
                JSONObject jsonObject = new JSONObject();
                jsonObject.accumulate("homeMobileCountryCode", 310);
                jsonObject.accumulate("homeMobileNetworkCode", 410);
                jsonObject.accumulate("radioType", "gsm");
                jsonObject.accumulate("carrier", "vodafone");

                JSONArray cellTowersArray = new JSONArray();
                JSONObject cellTowerObject = new JSONObject();
                cellTowerObject.accumulate("cellId", 42);
                cellTowerObject.accumulate("locationAreaCode", 415);
                cellTowerObject.accumulate("mobileCountryCode", 310);
                cellTowerObject.accumulate("mobileNetworkCode", 410);
                cellTowerObject.accumulate("age", 0);
                cellTowerObject.accumulate("signalStrength", -60);
                cellTowerObject.accumulate("timingAdvance", 15);
                cellTowersArray.put(cellTowerObject);
                jsonObject.accumulate("cellTowers", cellTowersArray);

                JSONArray wifiAccessPointsArray = new JSONArray();
                JSONObject wifiAccessPointObject = new JSONObject();
                wifiAccessPointObject.accumulate("macAddress", "01:23:45:67:89:AB");
                wifiAccessPointObject.accumulate("age", 0);
                wifiAccessPointObject.accumulate("channel", 11);
                wifiAccessPointObject.accumulate("signalToNoiseRatio", 40);
                wifiAccessPointsArray.put(wifiAccessPointObject);
                jsonObject.accumulate("wifiAccessPoints", wifiAccessPointsArray);

                // 4. convert JSONObject to JSON to String
                json = jsonObject.toString();


                // 5. set json to StringEntity
                StringEntity se = new StringEntity(json);

                // 6. set httpPost Entity
                httpPost.setEntity(se);

                // 7. Set some headers to inform server about the type of the content
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");

                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);

                // 9. receive response as inputStream
                inputStream = httpResponse.getEntity().getContent();

                // 10. convert inputstream to string
                if(inputStream != null)
                    result = convertInputStreamToString(inputStream);
                else
                    result = "Did not work";
            }
            catch (MalformedURLException e) {
                logException(e);
            }
            catch (IOException e) {
                logException(e);
            }
            catch (Exception e) {
                logException(e);
            }

            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(YourActivity.this, result, Toast.LENGTH_LONG).show();
        }
    }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
          BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
          String line = "";
          String result = "";
          while((line = bufferedReader.readLine()) != null)
              result += line;

          inputStream.close();
          return result;
    }   

您可以像这样在您的活动中调用此AsyncTask:

GetGeolocationTask getBlogPostsTask = new GetGeolocationTask();
getGeolocationTask.execute(); 

您也可以阅读本教程,以获取有关如何将JSON放入请求正文的详细信息。

或者,您可以使用Google Volley库执行您的网络请求,可以在此Stackoverflow答案中找到示例请求。

  ask by Rahul kumar translate from so

未解决问题?本站智能推荐: