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();

它的给连接拒绝异常。

4

1 回答 1

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

您还可以修改如何对 URL 进行编码以避免错误:

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");); 
于 2013-07-01T11:13:17.403 回答