Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android地理位置示例请求,未找到结果_Java_Android_Json_Google Maps_Google Geolocation - Fatal编程技术网

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

Java Android地理位置示例请求,未找到结果,java,android,json,google-maps,google-geolocation,Java,Android,Json,Google Maps,Google Geolocation,我想通过手机发射塔信息获取手机的估计位置。 我通过Android发布了来自Google Geolocation的示例请求。 我发布请求的代码中有什么错误? 我使用HTTPPOST通过将内容类型设置为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

我想通过手机发射塔信息获取手机的估计位置。 我通过Android发布了来自Google Geolocation的示例请求。 我发布请求的代码中有什么错误?

我使用HTTPPOST通过将内容类型设置为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();

    }
}

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

首先,您至少需要两个WIFI接入点

第二种解释:

如果响应为404,则表示您已确认无法定位wifiAccessPoints和cellTowers对象


所有字段都是可选的,因此请尝试使用无线接入点的空列表发送您的请求,然后使用手机发射塔的空列表查看问题的确切位置。

检查您的互联网连接,并使用其他连接和无线网络进行尝试。互联网连接良好,如果没有互联网连接,它甚至无法显示结果。
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;
    }

}
}