gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:40:09 25 4
gpt4 key购买 nike

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

所以当我在 postman 中执行请求时,效果很好。但是当我尝试在应用程序中执行相同的请求时,它给出的响应为响应代码 400。

根据开发者指南响应代码 400

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

显示当 api key 错误时出现。但是 key 如何在 postman 中而不是在应用程序中起作用?

这是服务器请求的代码:

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

关于java - 从地理位置 api 获取响应代码为 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48641846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com