Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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
Android 读取URL返回的文本文件_Android - Fatal编程技术网

Android 读取URL返回的文本文件

Android 读取URL返回的文本文件,android,Android,这个URL直接返回并打开文本文件,我只想读取它的内容,我怎么做 我试过了 public static String translate(String sl, String tl, String text) throws IOException{ // fetch URL url = new URL("https://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=" +

这个URL直接返回并打开文本文件,我只想读取它的内容,我怎么做

我试过了

public static String translate(String sl, String tl, String text) throws IOException{
        // fetch
        URL url = new URL("https://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=" +
                sl + "&tl=" + tl + "&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=" + 
                URLEncoder.encode(text, "UTF-8"));
        Log.d("URL", ":: "+url);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setRequestProperty("User-Agent", "Something Else");
        Log.d("URL", ":: After opening Connection");
        BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        Log.d("URL", ":: br "+br);
        String result = br.readLine();
        br.close();
        // parse
        Log.d("URL", ":: "+result);
        result = result.substring(2, result.indexOf("]]") + 1);
        StringBuilder sb = new StringBuilder();
        String[] splits = result.split("(?<!\\\\)\"");
        for(int i = 1; i < splits.length; i += 8)
            sb.append(splits[i]);
        return sb.toString().replace("\\n", "\n").replaceAll("\\\\(.)", "$1");
    }
公共静态字符串转换(字符串sl、字符串tl、字符串文本)引发IOException{
//取回
URL=新URL(“https://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=" +
sl+“&tl=“+tl+”&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=“+
编码(文本,“UTF-8”);
Log.d(“URL”,“URL:”+URL);
URLConnection URLConnection=url.openConnection();
setRequestProperty(“用户代理”、“其他内容”);
Log.d(“URL”,“::打开连接后”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(urlConnection.getInputStream());
Log.d(“URL”,::br“+br);
字符串结果=br.readLine();
br.close();
//解析
Log.d(“URL”,“:”+结果);
result=result.substring(2,result.indexOf(“]]”)+1);
StringBuilder sb=新的StringBuilder();

String[]splits=result.split((?如果您的Url直接打开的是文本文件,则此代码读取文本文件并打印如下:

public class URLReader {
public static void main(String[] args) throws Exception {

    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader in = new BufferedReader(
    new InputStreamReader(oracle.openStream()));

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

}

你能解释一下你到目前为止都做了些什么吗