2015-02-09 56 views
0

當我使用谷歌的地方API以自動完成textview.After研究我已經做了application.But當我運行該應用程序我得到下面的錯誤的地方得到錯誤使用谷歌API的地方

ERROR

This IP, site or mobile application is not authorized to use this API key 

CODE

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

PS-我知道這已經被回答指出,美不需要Android的關鍵,但你服務器密鑰,所以,我是用我的IP地址,但仍是同樣的錯誤。

+0

使用google api控制檯爲您的應用程序生成api密鑰並使用該api密鑰 – 2015-02-09 08:15:42

回答

0

請檢查link並創建一個項目&生成API密鑰。使用下面一行API密鑰:

private static final String API_KEY = "key=My Server Key"; 

爲了您上面的代碼的API密鑰將服務器API密鑰,而不是Android應用程序API密鑰。如果您仍然發現任何問題,請告訴我。

+0

是的,先生我已經提到過我使用本地IP地址生成服務器密鑰 – Anuj 2015-02-09 08:32:47

+0

請不要在此提供任何IP地址。只需空白框並生成。 – sUndeep 2015-02-09 08:35:24

+0

ok lemme試試看 – Anuj 2015-02-09 08:37:37