Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建的url可以工作,但当应用程序发送时会出错_Java_Google Maps - Fatal编程技术网

Java 创建的url可以工作,但当应用程序发送时会出错

Java 创建的url可以工作,但当应用程序发送时会出错,java,google-maps,Java,Google Maps,使用我的应用程序发送以下URL时,我遇到以下错误: 服务器返回HTTP响应代码:400,URL:Centre+308 George Street+Sydney&destinations=科学博物馆展览路伦敦SW7 2DD&模式=驾驶&传感器=错误 但是在浏览器中输入URL会发现它是正确的 在获取url之前,您可能需要对其进行url编码。查看一下您可以在打开连接读取数据之前对请求URL进行编码。为了更好地理解,请查看以下代码: package com.stackoverflow.works; i

使用我的应用程序发送以下URL时,我遇到以下错误:

服务器返回HTTP响应代码:400,URL:Centre+308 George Street+Sydney&destinations=科学博物馆展览路伦敦SW7 2DD&模式=驾驶&传感器=错误


但是在浏览器中输入URL会发现它是正确的

在获取url之前,您可能需要对其进行url编码。查看一下

您可以在打开连接读取数据之前对请求URL进行编码。为了更好地理解,请查看以下代码:

package com.stackoverflow.works;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;

public class URLReader {
    /*
     * @author: sarath_sivan
     */

    public static void read(String url) throws IOException {
        setProxy();//only invoke this method if you are using any proxy to open connection
        URL httpURL = new URL(url);
        BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(httpURL.openStream()));

        String inputLine;
        while ((inputLine = bufferedReader.readLine()) != null) {
            System.out.println(inputLine);
        }   
        bufferedReader.close();
    }

    public static void setProxy() {
        System.getProperties().put("http.proxyHost", "xxx.xxx.xx.xx");//replace with your proxy
        System.getProperties().put("http.proxyPort", "8080");
    }

    public static String encodeURL(String url) throws UnsupportedEncodingException {//encoding your request url parameters here
        StringBuilder encodedURL = new StringBuilder(url);
        encodedURL.append("?origins=").append(encode("Medical Centre+ 308 George Street+ Sydney"));
        encodedURL.append("&destinations=").append(encode(" Science Museum Exhibition Road London SW7 2DD"));
        encodedURL.append("&mode=").append("driving");
        encodedURL.append("&sensor=").append("false");
        return encodedURL.toString();
    }

    public static String encode(String string) throws UnsupportedEncodingException {
        return URLEncoder.encode(string, "ISO-8859-1");
    }

    public static void main(String[] args) throws IOException {
        String url = "http://maps.googleapis.com/maps/api/distancematrix/xml";
        read(encodeURL(url));
    }

}
输出如下所示:

您使用的谷歌地图版本是什么?你能分享一下你的java代码快照吗。