Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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 读取下载的文本文件-FileNotFoundException_Java_Android_Json_Filenotfoundexception - Fatal编程技术网

Java 读取下载的文本文件-FileNotFoundException

Java 读取下载的文本文件-FileNotFoundException,java,android,json,filenotfoundexception,Java,Android,Json,Filenotfoundexception,plz帮助,我正在尝试从这个谷歌翻译API URL获取数据 只有当值为1个单词时,它才起作用。。如果是2,我就错了 我的意思是,这将有效: String sourceLang = "auto"; String targetLang = "en"; String sourceText = "olas"; String urlstring = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + sour

plz帮助,我正在尝试从这个谷歌翻译API URL获取数据 只有当值为1个单词时,它才起作用。。如果是2,我就错了

我的意思是,这将有效:

String sourceLang = "auto";
String targetLang = "en";
String sourceText = "olas";
String urlstring = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + sourceLang + "&tl=" + targetLang + "&dt=t&q=" + sourceText;
但如果我用两个词来表达:

String sourceText = "olas olas";
它将给我filenotfoundexception错误

代码如下:

        URL url = new URL(urlstring);
        HttpURLConnection httpURLconnection = (HttpURLConnection) url.openConnection();
        httpURLconnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
        InputStream inputStream = httpURLconnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while(line != null){
            line = bufferedReader.readLine();
            data = data + line;
        }

将空间替换为%20,如

urlstring=urlstring.replace(" ", "%20");

URL url = new URL(urlstring);
        HttpURLConnection httpURLconnection = (HttpURLConnection) url.openConnection();
        httpURLconnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
        InputStream inputStream = httpURLconnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while(line != null){
            line = bufferedReader.readLine();
            data = data + line;
        }

url中不能有空格,这就是它出现错误的原因。请确保您有有效的文件名