Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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_Url - Fatal编程技术网

如何在android中从url读取文本文件

如何在android中从url读取文本文件,android,url,Android,Url,我想写一个应用程序,需要从服务器接收一些文本 这是我的代码: String db = "" new Thread(new Runnable() { @Override public void run() { try { // Create a URL for the desired page URL url = new URL("htt

我想写一个应用程序,需要从服务器接收一些文本 这是我的代码:

String db = ""
new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    // Create a URL for the desired page
                    URL url = new URL("http://chemvaaj.xzn.ir/test/words.txt");

                    // Read all the text returned by the server
                    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                    String str;
                    while ((str = in.readLine()) != null) {
                        // str is one line of text; readLine() strips the newline character(s)
                        db+=str;
                    }
                    in.close();
                } catch (MalformedURLException e) {
                } catch (IOException e) {
                }

            }
        }).start();

        searchEditText.setText(dbd);
这似乎是正确的,但变量db最后仍然是“”。

请尝试以下代码:

希望这能解决你的问题

注意:首先,您必须在另一个线程中进行联网操作,因为主线程中的联网会使您的应用程序在任何请求期间都无响应。所以把这个代码放进去


“但它不起作用”。认真地400多次重复,我们得到的错误是“它不工作”?没有运行时或编译错误!这是一个逻辑错误!变量db最后是空的!我不知道如何修复它这个“dbd”变量是什么?您添加了internet权限吗?它真的是空的吗?您在后台运行操作,但尝试在线程启动后立即设置文本似乎您不了解多线程的基本知识。。。正如您所看到的,输出是1-3-2而不是1-2-3。。。您也有相同的情况数据为空:|请尝试其他文件并进行测试。你会知道到底是什么问题。
DefaultHttpClient  httpclient = new DefaultHttpClient();

HttpGet httppost = new HttpGet("http://chemvaaj.xzn.ir/test/words.txt");
HttpResponse response = httpclient.execute(httppost);
        HttpEntity ht = response.getEntity();

        BufferedHttpEntity buf = new BufferedHttpEntity(ht);

        InputStream is = buf.getContent();


        BufferedReader r = new BufferedReader(new InputStreamReader(is));

        StringBuilder text = new StringBuilder();
        String data;
        while ((data= r.readLine()) != null) {
            text .append(line + "\n");
        }

        searchEditText.setText(text );