从URL获取文本并显示它(几乎工作) [英] Getting text from a url and displaying it (Almost works)

查看:105
本文介绍了从URL获取文本并显示它(几乎工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从URL中获取文本并显示为字符串。网址.TXT结束,因为在www.gains.com/more.txt这些文本很长,他们有一个1MB的最大尺寸。我试图把它与的AsyncTask 。问题是,code,有时工作。它的工作我第一次跑了code中的第二次它没有显示的文字。有时,应用程序有时会显示的文本不会。这里发生了什么?这里是我的code。

I am trying to get text from urls and display them as strings. Urls end with .txt as in www.gains.com/more.txt These text are long and they have a maximum size of 1MB. I am trying to it with AsyncTask. The problem is that the code sometimes works. It worked the first time I ran the code the second time it didn't display the text. Sometimes the app would display the text sometimes it wouldn't. What is going on here? Here is my code.

class RequestTask extends AsyncTask<String, String, String>{

        @Override
        // username, password, message, mobile
        protected String doInBackground(String... url) {
            // constants
            int timeoutSocket = 5000;
            int timeoutConnection = 5000;

            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
            HttpClient client = new DefaultHttpClient(httpParameters);

            HttpGet httpget = new HttpGet(url[0]);

            try {
                HttpResponse getResponse = client.execute(httpget);
                final int statusCode = getResponse.getStatusLine().getStatusCode();

                if(statusCode != HttpStatus.SC_OK) {
                    Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
                    return null;
                }

                String line = "";
                StringBuilder total = new StringBuilder();

                HttpEntity getResponseEntity = getResponse.getEntity();

                BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));  

                while((line = reader.readLine()) != null) {
                    total.append(line);
                }

                line = total.toString();
                story.add(line); //story is my array i use to display the text
                return line;
            } catch (Exception e) {
                Log.w("MyApp", "Download Exception : " + e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            //This is empty i dont know what it does
        }
        }

这是我如何称呼它

Here is how i call it

new RequestTask().execute("www.gains.com/more.txt");

也有另一个较小的问题,IM是显示文字时,我失去了文本格式作为我失去一个得到一个巨大的段落的段落之间的空间。有没有办法解决这个问题的方法吗?我应该用另一种方法?

Also another smaller problem im having is that when the text is displayed I lose the format of the text as in i lose the spaces between paragraphs an get one huge paragraph. Is there a way to solve this? Should I use another method?

推荐答案

调用显示功能将显示在文件下载后保存在文件中的文本。

Call the display function that will display the text from the file saved after the file is downloaded.

使用onPostExecute

Use onPostExecute

    @Override
    protected void onPostExecute(String result) {
        //You add the code where you call the text from the file saved
    }

您可以从调用线程的主要活动调用一个函数。

You can call a function from the main activity that calls the thread.

这样,视图将只显示,直到下载并保存的文本完成的过程。

This way, the view will only be displayed until the process of downloading and saving your text is finished.

编辑:下面是一个示例

    TextPage textPage; // the activity that calls the AsyncTask
List<String> story = new ArrayList<String>();

GetTextInfoTask(TextPage textPage) {
    this.textPage = textPage;
}
    ... // your doInBackground function here

@Override
protected void onPostExecute(Object objR){
    // A toast is displayed in the TextPage Activity once the data is finished downloading
    Toast.makeText(textPage.getBaseContext(), story.get(0),
            Toast.LENGTH_LONG).show();
}

这篇关于从URL获取文本并显示它(几乎工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆