我正在尝试连接到谷歌地方api,但它给了我错误: “此IP,站点或移动应用程序无权使用此API密钥。请求从IP地址183.87.41.123收到,请求空引用”,“预测”:[],“状态”:“REQUEST_DENIED”}
我已经使用了所有三种类型的API,但是我使用的是Android密钥,而不是我使用的是Android密钥,现在我使用的是服务器密钥,但没有结果!
这是我的代码:
package com.astro.famouspandit.Activities.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.astro.famouspandit.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class Location extends AppCompatActivity {
private EditText mEdttxtCity;
private String input;
String API_KEY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
//API_KEY = "sdlflfNK-sldkfndlsnfdslanflsdn";
API_KEY = "fsdnflsdnflnflsdnfldsnfldnfldnf";
//browser key API_KEY = "askjskfdskjfssdkfksdjbfsdk-csdkfdskn";
mEdttxtCity = (EditText)findViewById(R.id.edttxtCity);
mEdttxtCity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
input = mEdttxtCity.getText().toString();
if (input.length() >3) {
SearchPlacesTask searchplacesTask = new SearchPlacesTask();
searchplacesTask.execute();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private class SearchPlacesTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("SearchPlacesTask Called","");
}
@Override
protected String doInBackground(String... place) {
// For storing data from web service
String data = "";
// Obtain browser key from https://code.google.com/apis/console
String key = "key="+API_KEY;
try {
input = "input=" + URLEncoder.encode(input, "utf-8");
Log.e("input String : ","input" + input);
} catch (Exception e1) {
e1.printStackTrace();
}
// place type to be searched
String types = "types=geocode";
// Sensor enabled
String sensor = "language=en";
/*// Building the parameters to the web service
String parameters = input + "&" + types + "&" + sensor + "&" + key;*/
// Building the parameters to the web service
String parameters = input + "&" + sensor + "&" + key;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"
+ output + "?" + parameters;
Log.d("Final URL Google API : ","url" + url);
// String url = "https://maps.googleapis.com/maps/api/place/autocomplete/location=13.758662,100.496443" + output + "?" + parameters;
try {
// Fetching the data from we service
data = downloadUrlsearch(url);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//YOUR JSON DATA HERE.
//NOW YOU CAN PARSE.
}
}
private String downloadUrlsearch(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// 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);
}
data = sb.toString();
Log.d("data: ","" + data);
br.close();
} catch (Exception e) {
e.printStackTrace();
// Log.d("Exception while downloading url", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
}
答案 0 :(得分:-1)
确保在生成API时,某些包或签名没有特定的API?