从地理位置 api 获取响应代码 400

Getting Response code as 400 from geo location api

我正在尝试使用地理位置 api 来获取位置的纬度和经度。为此,我在开发人员控制台上创建了一个项目并创建了一个 api 键。我用这个 api 键和 api https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY

所以当我在邮递员中执行请求时,它运行良好。 但是当我试图在一个应用程序中执行相同的请求时,它给出的响应是响应代码 400。

根据开发人员指南响应代码 400

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

表示api键错误时出现。但是密钥如何在邮递员中而不是在应用程序中工作?

服务器请求代码如下:

    public JSONObject sendPostRequest1(String data) {

    try {

        URL url = new URL(api);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
      //  con.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        con.setDoOutput(true);
        con.setDoInput(true);
        OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());

        try {
            writer.write(data);
        } catch (Exception e) {
            Log.e("Exception111", e.toString());
        }

        writer.close();

        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            Log.d("ServerResponse", new String(sb));
            String output = new String(sb);
            return new JSONObject(output);
        } else {
            Log.e("Exception", "" + responseCode);
        }
    }
    catch (JSONException je)
    {
        je.printStackTrace();
        return Excpetion2JSON.getJSON(je);
    }
    catch(IOException e)
    {

    }
    return null;
}

异步任务:

public class GetLocationAsyncTask extends AsyncTask<String, Void, JSONObject> {

    String api;
    JSONObject jsonParams;
    Context mContext;
    private ProgressDialog loadingDialog;
    private String number,code;
    public GetLocationsCallBack getLocationsCallBack;

    public GetLocationAsyncTask(Context context,GetLocationsCallBack getLocationsCallBack) {

        this.mContext = context;
        this.getLocationsCallBack = getLocationsCallBack;

    }

    public interface GetLocationsCallBack {
        void doPostExecute(ArrayList<Location> list);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        try {

            api = "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyCArRAX4oHdfFWrTWhXrOVBQtbs";

            jsonParams = new JSONObject();
            jsonParams.put("cellId", params[0]);
            jsonParams.put("locationAreaCode",params[1]);

            ServerRequest request = new ServerRequest(api, jsonParams);
            return request.sendPostRequest1(jsonParams.toString());

        } catch (Exception ue) {
            return Excpetion2JSON.getJSON(ue);
        }
    }  //end of doInBackground

    @Override
    protected void onPostExecute(JSONObject response) {
        super.onPostExecute(response);


            if(response.has("location"))
            {

                try {

                    Location location = new Location();

                    location.setLattitude(response.getString("lat"));
                    location.setLongitude(response.getString("lng"));

                    ArrayList<Location> locations = location.getLocationArrayList();

                    locations.add(location);
                }
                catch (JSONException je)
                {
                    Log.d("JsonException",je.toString());
                }

            }

        if (loadingDialog.isShowing())
            loadingDialog.dismiss();

    }
}

正在执行异步任务:

        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

        int cellid= cellLocation.getCid();
        int celllac = cellLocation.getLac();


        Log.d("CellLocation", cellLocation.toString());
        Log.d("GSM CELL ID",  String.valueOf(cellid));
        Log.d("GSM Location Code", String.valueOf(celllac));

        GetLocationAsyncTask getLocationAsyncTask = new GetLocationAsyncTask(MainActivity.this,MainActivity.this);
        getLocationAsyncTask.execute(String.valueOf(cellid),String.valueOf(celllac));

这是怎么回事?请帮忙。谢谢..

你必须告诉接收方你发送的是json

con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");