Android地理定位示例请求,未找到结果

时间:2014-06-03 07:19:36

标签: java android json google-maps google-geolocation

我希望通过Cell Towers信息获取手机的估算位置。 我通过Android从Google地理位置发布了示例请求。 我的代码发布请求有什么问题? 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;
    }

}
}

1 个答案:

答案 0 :(得分:0)

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

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

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

第二种解释:

如果响应为404,则说明您无法确定wifiAccessPoints和cellTowers对象的地理位置。

所有字段都是可选的,因此请尝试向您的请求发送一个Wifi接入点的空白列表,然后再发送一个空白的Cell Towers列表,以查看问题出在哪里。