void url_get(double lati,double longi) {
URL url = null;
try {
url = new URL("https://maps.googleapis.com/maps/api/place/search/json?&location="+lati+","+longi+"&radius=1000&types=hospital&sensor=true&key=MY_KEY);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
TextView json = (TextView) findViewById(R.id.json3);
String theString = IOUtils.toString(in, "UTF-8");
json.setText(theString);
//System.out.println(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
当我对纬度和经度值进行硬编码时,代码运行正常。但是当我尝试通过函数传递latitutde和longitude的值时,它得到了识别。我认为URL没有正确构建。请问你好吗?建议一种方法来建立我的网址与纬度和经度的值?
答案 0 :(得分:0)
您应该使用以下方法将值转换为字符串:
Double.toString(double);
或:
String.valueOf(double);
所以你的字符串应该是这样的:
url = new URL("https://maps.googleapis.com/maps/api/place/search/json?&location="+String.valueOf(lati)+","+String.valueOf(longi)+"&radius=1000&types=hospital&sensor=true&key=MY_KEY");
在您的代码中,字符串末尾缺少"
。