使用 google 地方 api 时出现错误
Getting error when using google place api
我已经使用 google 地方 api 来获取自动完成的地方 textview.After 研究 我已经 application.But 当我 运行 申请我得到以下错误
错误
This IP, site or mobile application is not authorized to use this API key
代码
public class GetPlaces extends AsyncTask<String, String, String> {
private Context context;
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "key=My Server Key";
private String INPUT = "INPUT";
private HttpURLConnection urlConnection = null;
private StringBuilder stringBuilder = null;
private URL url;
private InputStream iStream = null;
public GetPlaces(Context context, String INPUT) {
this.context = context;
this.INPUT = INPUT;
this.execute(INPUT);
}
@Override
protected String doInBackground(String... params) {
String response = "";
try {
INPUT = "input=" + URLEncoder.encode(params[0], "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String types = "types=geocode";
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = INPUT + "&" + types + "&" + API_KEY + "&" + sensor;
stringBuilder = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
stringBuilder.append("?" + parameters);
//connections is established here
try {
url = new URL(stringBuilder.toString());
urlConnection = (HttpURLConnection) url.openConnection();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
response = sb.toString();
Log.e("Response", response);
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
iStream.close();
} catch (IOException e) {
e.printStackTrace();
}
urlConnection.disconnect();
}
return response;
}
}
P.S-我知道这已经被回答说你不需要 Android 密钥但是你服务器密钥,所以为此我使用了我的 IP 地址但仍然是同样的错误。
请检查 link 并创建项目并生成 API 密钥。使用下面一行的 API 键:
private static final String API_KEY = "key=My Server Key";
对于您上面的代码,API 键将是服务器 API 键,而不是 Android 应用程序 API 键。如果您仍然发现任何问题,请告诉我。
我已经使用 google 地方 api 来获取自动完成的地方 textview.After 研究 我已经 application.But 当我 运行 申请我得到以下错误
错误
This IP, site or mobile application is not authorized to use this API key
代码
public class GetPlaces extends AsyncTask<String, String, String> {
private Context context;
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "key=My Server Key";
private String INPUT = "INPUT";
private HttpURLConnection urlConnection = null;
private StringBuilder stringBuilder = null;
private URL url;
private InputStream iStream = null;
public GetPlaces(Context context, String INPUT) {
this.context = context;
this.INPUT = INPUT;
this.execute(INPUT);
}
@Override
protected String doInBackground(String... params) {
String response = "";
try {
INPUT = "input=" + URLEncoder.encode(params[0], "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String types = "types=geocode";
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = INPUT + "&" + types + "&" + API_KEY + "&" + sensor;
stringBuilder = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
stringBuilder.append("?" + parameters);
//connections is established here
try {
url = new URL(stringBuilder.toString());
urlConnection = (HttpURLConnection) url.openConnection();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
response = sb.toString();
Log.e("Response", response);
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
iStream.close();
} catch (IOException e) {
e.printStackTrace();
}
urlConnection.disconnect();
}
return response;
}
}
P.S-我知道这已经被回答说你不需要 Android 密钥但是你服务器密钥,所以为此我使用了我的 IP 地址但仍然是同样的错误。
请检查 link 并创建项目并生成 API 密钥。使用下面一行的 API 键:
private static final String API_KEY = "key=My Server Key";
对于您上面的代码,API 键将是服务器 API 键,而不是 Android 应用程序 API 键。如果您仍然发现任何问题,请告诉我。