android谷歌api从地址获取lat长

时间:2016-01-15 15:00:24

标签: android google-maps-api-3 httpurlconnection

我正试图在我的Android应用程序中获取lat和long。我收到了各种错误,最后一个是javax.net.ssl.SSLHandshakeException: Handshake failed

这是我的代码,我尝试通过检查网络来做某事,但它不起作用。

private static final String getLatLongUrl = "https://maps.googleapis.com/maps/api/geocode/xml?address=#ADDRESS#&key=";

public String getLatLong(){
    String json = "{\"key\":1}";

    String urlS = getLatLongUrl;
    urlS = urlS.replace("#ADDRESS#", address);
    urlS += apiKey;


    String json = "{\"key\":1}";
    try {
        URL url = new URL(urlS);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(json);
        wr.flush();
        wr.close();

        String tag[] = { "lat", "lng" };

        InputStream stream = conn.getInputStream();
        conn.disconnect();

        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document doc = builder.parse(stream);
        if (doc != null) {
            NodeList nl;
            ArrayList args = new ArrayList();
            for (String s : tag) {
                nl = doc.getElementsByTagName(s);
                if (nl.getLength() > 0) {
                    Node node = nl.item(nl.getLength() - 1);
                    args.add(node.getTextContent());
                } else {
                    args.add(" - ");
                }
            }

            String result = "lat:" + args.get(0) + " long:" + args.get(1);
            return result;
        }

    } catch(Exception ex){
        return "";
    }
    return "";
}

可能我失败了,但我无法弄清楚是什么,这是我的第一次httlUrlConnectionGoogleApi。感谢所有

,我们将不胜感激

1 个答案:

答案 0 :(得分:0)

由于您的网址前缀为Https,因此您应该使用UrlConnection

http://developer.android.com/training/articles/security-ssl.html

相关问题