2016-07-19 151 views
0

我用這個tutorial在autocompletetextview的建議得到地址。我激活了Google Places API並生成了Android密鑰。但是當我運行該應用程序時,出現此錯誤This IP, site or mobile application is not authorized to use this API key. Request received from IP address <IP>, with empty referer。有什麼我做錯了嗎?如果有人能夠幫助我,那將會很棒。謝謝。谷歌的地方API授權錯誤

I got this error as link in logcat

credentials

public class MainActivity extends Activity implements AdapterView.OnItemClickListener { 
 

 
    private static final String LOG_TAG = "ExampleApp"; 
 

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

 
    //------------ make your specific key ------------ 
 
    private static final String API_KEY = "<GooglePlaceAndroidKey as in credentials>"; 
 

 
    @Override 
 
    public void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.query); 
 

 
     autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item)); 
 
     autoCompView.setOnItemClickListener(this); 
 
    } 
 

 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
 
     String str = (String) adapterView.getItemAtPosition(position); 
 
     Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); 
 
    } 
 

 
    public static ArrayList<String> autocomplete(String input) { 
 
     ArrayList<String> resultList = null; 
 

 
     HttpURLConnection conn = null; 
 
     StringBuilder jsonResults = new StringBuilder(); 
 
     try { 
 
      StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON); 
 
      sb.append("?key=" + API_KEY); 
 
      sb.append("&components=country:gr"); 
 
      sb.append("&input=" + URLEncoder.encode(input, "utf8")); 
 

 
      URL url = new URL(sb.toString()); 
 

 
      System.out.println("URL: "+url); 
 
      conn = (HttpURLConnection) url.openConnection(); 
 
      InputStreamReader in = new InputStreamReader(conn.getInputStream()); 
 

 
      // Load the results into a StringBuilder 
 
      int read; 
 
      char[] buff = new char[1024]; 
 
      while ((read = in.read(buff)) != -1) { 
 
       jsonResults.append(buff, 0, read); 
 
      } 
 
     } catch (MalformedURLException e) { 
 
      Log.e(LOG_TAG, "Error processing Places API URL", e); 
 
      return resultList; 
 
     } catch (IOException e) { 
 
      Log.e(LOG_TAG, "Error connecting to Places API", e); 
 
      return resultList; 
 
     } finally { 
 
      if (conn != null) { 
 
       conn.disconnect(); 
 
      } 
 
     } 
 

 
     try { 
 

 
      // Create a JSON object hierarchy from the results 
 
      JSONObject jsonObj = new JSONObject(jsonResults.toString()); 
 
      JSONArray predsJsonArray = jsonObj.getJSONArray("predictions"); 
 

 
      // Extract the Place descriptions from the results 
 
      resultList = new ArrayList<String>(predsJsonArray.length()); 
 
      for (int i = 0; i < predsJsonArray.length(); i++) { 
 
       System.out.println(predsJsonArray.getJSONObject(i).getString("description")); 
 
       System.out.println("============================================================"); 
 
       resultList.add(predsJsonArray.getJSONObject(i).getString("description")); 
 
      } 
 
     } catch (JSONException e) { 
 
      Log.e(LOG_TAG, "Cannot process JSON results", e); 
 
     } 
 

 
     return resultList; 
 
    } 
 

 
    class GooglePlacesAutocompleteAdapter extends ArrayAdapter<String> implements Filterable { 
 
     private ArrayList<String> resultList; 
 

 
     public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) { 
 
      super(context, textViewResourceId); 
 
     } 
 

 
     @Override 
 
     public int getCount() { 
 
      return resultList.size(); 
 
     } 
 

 
     @Override 
 
     public String getItem(int index) { 
 
      return resultList.get(index); 
 
     } 
 

 
     @Override 
 
     public Filter getFilter() { 
 
      Filter filter = new Filter() { 
 
       @Override 
 
       protected FilterResults performFiltering(CharSequence constraint) { 
 
        FilterResults filterResults = new FilterResults(); 
 
        if (constraint != null) { 
 
         // Retrieve the autocomplete results. 
 
         resultList = autocomplete(constraint.toString()); 
 

 
         // Assign the data to the FilterResults 
 
         filterResults.values = resultList; 
 
         filterResults.count = resultList.size(); 
 
        } 
 
        return filterResults; 
 
       } 
 

 
       @Override 
 
       protected void publishResults(CharSequence constraint, FilterResults results) { 
 
        if (results != null && results.count > 0) { 
 
         notifyDataSetChanged(); 
 
        } else { 
 
         notifyDataSetInvalidated(); 
 
        } 
 
       } 
 
      }; 
 
      return filter; 
 
     } 
 
    } 
 

 
}

+0

你使用serverkey的地方API? –

回答

1

尚未啓用您地理編碼服務。要獲取Google地方信息,需要啓用地理編碼。

Google Console

選擇Google Maps Geocoding API和啓用它。

+0

我通過使用服務器密鑰而不是android密鑰解決了錯誤。感謝您,我也啓用了「Google地圖地理編碼API」。但現在的問題是我只能得到具體地點的建議。當我搜索德里時,我沒有得到任何東西。我也添加了截圖。 @Pratik – CodeAssasins

+0

您是否可以嘗試刪除API密鑰參數並進行WS調用? –

+0

我試過,但得到錯誤信息'此服務需要一個API key.' @Pratik – CodeAssasins

-1

接受 的問題是,你不使用針對Android的谷歌Places API的,您使用的是谷歌Places API的Web服務。

Herehere是使用谷歌Places API的Web服務的一個例子。你肯定使用後者。

啓用谷歌Places API的Web服務,它會工作:

+0

我啓用了'谷歌Places API的網絡Service',但它仍然顯示了同樣的錯誤@Jignesh – CodeAssasins

+0

這看起來就像我的文字在[另一個答案]說(http://stackoverflow.com/a/31014444)...此外,在這種情況下,這是錯誤的...... –

+0

我正在使用服務器密鑰,並且啓用了'Google地圖地理編碼API','Google Places API for Android'和'Google Places API Web Service'。您可以查看截圖(http://imgur.com/1BuMNTO)獲取當前結果。你能解釋爲什麼它只顯示特定的地方嗎?我也想從印度來的地方。 @DanielNugent – CodeAssasins