2013-07-01 66 views
1
URL url = new URL("http://maps.google.com/maps/api/geocode/json?address=1600%20Amphitheatre%20Parkway&sensor=false&client_id=my_client_id&key=my_key"); 
URLConnection urlConnection = url.openConnection(); 

HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; 
httpURLConnection.setDoInput(true); 
httpURLConnection.setRequestMethod("GET"); 

InputStream in = httpURLConnection.getInputStream(); 

其給予連接拒絕異常。在java中使用地理編碼(GoogleMapAPI)進行地址驗證

+0

您的密鑰和客戶端ID是prooper嘗試打在瀏覽器正確的URL和檢查你是否得到迴應? –

+0

在瀏覽器上粘貼上面的URL,正在給REQUEST_DENIED json .. – sanbhat

+0

@sanbhat他沒有給我們他的密鑰,如果你使用該鏈接的密鑰不匹配,所以你得到REQUEST_DENIED。 – maqjav

回答

0

你忘了實際連接:

URL url = new URL("yoururl"); 
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
httpURLConnection.setDoInput(true); 
httpURLConnection.setRequestMethod("GET"); 

try { 
    // open the connection and get results as InputStream. 
    httpURLConnection.connect(); 
    InputStream in = httpURLConnection.getInputStream();; 

    // do more things 
} finally { 
    httpURLConnection.disconnect(); 
} 

您也可以修改你如何編碼您的網址,以避免發生錯誤:

private String REQUEST_URL = "http://maps.google.com/maps/api/geocode/json"; 
private String address = "1600 Amphitheatre Parkway"; 

URL url = new URL(REQUEST_URL + "?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=false&client_id=my_client_id&key=my_key");); 
相關問題