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

标签 java android json filenotfoundexception

请帮忙,我正在尝试从此谷歌翻译 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;
        }

关于java - 读取下载的文本文件 - FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51598585/

相关文章:

java - Java 静态调用比非静态调用更昂贵还是更便宜?

Android NativeActivity + 其他 Activity

android - fragment 对话框在关闭时为布局设置动画

java - GSON 库忽略值字段中的 + 符号

混合了文字和数组的 Javascript 数组

java - 在屏幕上移动对象

java - 尝试在 Apache Tomcat 上部署 GWT 失败

java - 更改 gui 中的面板

java - 当 Activity 关闭时,来自服务的 Android 调用监听器将不会响应

javascript - 如何在 Typescript 中更新数组列表的对象?