试图在android系统读取文本文件中的AsyncTask,但我得到空的结果 [英] Trying to read text file in AsyncTask in android, but i get empty result

查看:151
本文介绍了试图在android系统读取文本文件中的AsyncTask,但我得到空的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读在一个机器人类的AsyncTask网页的文本文件,然后将结果传递到一个静态变量,然后将其加载到另一个班级一个TextView,但我得到空的结果不能弄明白为什么。很明显,我在我的code搞乱的东西...

谁能审查呢?在此先感谢!

 包com.example.cuccandroid;进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.net.MalformedURLException;
进口的java.net.URL;进口android.os.AsyncTask;公共类readtextfile扩展的AsyncTask<字符串,整数,太虚> {    @覆盖
    保护无效doInBackground(字符串... PARAMS){
        尝试{               网址URL =新的URL(http://example.com/example.txt);            在的BufferedReader =新的BufferedReader(新的InputStreamReader(url.openStream()));
            串线= NULL;
            而((行= in.readLine())!= NULL){
                //获取行
                Kezdoh.descriptiontext =行;
            }
            附寄();
            }赶上(MalformedURLException的E){                e.printStackTrace();
            }赶上(IOException异常五){                e.printStackTrace();
            }
        返回null;
    }     保护无效onProgressUpdate(){
        //当后台任务使任何进展称为
     }      在preExecute保护无效(){
         //称为doInBackground()被启动之前
     }
     保护无效onPostExecute(){
         //称为doInBackground()完成后
     }
      }

我的班,在那里我尝试加载

 包com.example.cuccandroid;进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口com.example.cluppandroid.R;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
公共类Kezdoh扩展片段{    TextView的描述;
    静态字符串descriptiontext描述;
  @覆盖
      公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
          捆绑savedInstanceState){
          查看机器人= inflater.inflate(R.layout.kezdoh,集装箱,FALSE);
        // GetDescription();
        说明=((的TextView)android.findViewById(R.id.description1));        新readtextfile()执行(http://example.com/example.txt);
        description.setText(descriptiontext描述);
        返回机器人;
        }
}


解决方案

您必须记住的AsyncTask在不同的线程在不同的时间执行。

在您的情况下会发生什么事是description.setText(descriptiontext描述);首先执行。

一个好的办法是preventing使用descriptiontext描述的静态引用。

什么,你可以在这种情况下,做的是增加一个构造函数来readtextfile并传递TextView的实例。
onPostExecute()方法将doInBackground()之后执行的读取的字符串的内容。
任何UI相关的方法最好在onPostExecute()方法来访问。

我稍微修改您的readtextfile类,如下所示:

 公共类readtextfile扩展的AsyncTask<字符串,整数,字符串> {私人TextView的textViewToSet;
公共readtextfile(TextView的descriptionTextView){
    this.textViewToSet = descriptionTextView;
    }@覆盖
保护字符串doInBackground(字符串... PARAMS){
    字符串结果=;
    尝试{
           网址URL =新的URL(http://example.com/example.txt);        在的BufferedReader =新的BufferedReader(新的InputStreamReader(url.openStream()));
        串线= NULL;        而((行= in.readLine())!= NULL){
            //获取行
            结果+ =行;
        }
        附寄();
        }赶上(MalformedURLException的E){            e.printStackTrace();
        }赶上(IOException异常五){            e.printStackTrace();
        }
    返回结果;
} 保护无效onProgressUpdate(){
    //当后台任务使任何进展称为
 }  在preExecute保护无效(){
     //称为doInBackground()被启动之前
 }@覆盖
 保护无效onPostExecute(字符串结果){
     this.textViewToSet.setText(结果);
 }
  }

您需要做的,下一步是只需要调用

 新readtextfile()执行(http://example.com/example.txt);

和来自onCreateView方法删除此行

  description.setText(descriptiontext描述);

I'm trying to read a text file from the web in an AsyncTask class in android, then pass the result into a static variable, and then load it to a TextView in another class, but i'm get empty results and can't figure it out why. Obviously i'm messing up with something in my code...

Could anyone review this? Thanks in advance!

package com.example.cuccandroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.AsyncTask;

public class readtextfile extends AsyncTask<String, Integer, Void>{

    @Override
    protected Void doInBackground(String... params) {
        try {

               URL url = new URL("http://example.com/example.txt");       

            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                //get lines
                Kezdoh.descriptiontext=line;
            }
            in.close();


            } catch (MalformedURLException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }
        return null;
    }

     protected void onProgressUpdate() {
        //called when the background task makes any progress
     }

      protected void onPreExecute() {
         //called before doInBackground() is started
     }
     protected void onPostExecute() {
         //called after doInBackground() has finished 
     }
      }

My class, where im trying to load

package com.example.cuccandroid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import com.example.cluppandroid.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Kezdoh extends Fragment{

    TextView description;
    static String descriptiontext;
  @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View android = inflater.inflate(R.layout.kezdoh, container, false);
        //GetDescription();
        description = ((TextView)android.findViewById(R.id.description1));

        new readtextfile().execute("http://example.com/example.txt");
        description.setText(descriptiontext);
        return android;
        }   
}

解决方案

You have to remember that AsyncTask is executed in different thread at different time.

What happens in your case is description.setText(descriptiontext); is executed first.

A good approach would be preventing to use static reference of descriptiontext.

What you could do in this case is to add a constructor to readtextfile and pass the TextView instance. onPostExecute() method would be executed after doInBackground() that read the string content. Any UI related methods are best be accessed in onPostExecute() method.

I have slightly modified your readtextfile class as the following:

public class readtextfile extends AsyncTask<String, Integer, String>{

private TextView textViewToSet;
public readtextfile(TextView descriptionTextView){
    this.textViewToSet = descriptionTextView;
    }

@Override
protected String doInBackground(String... params) {
    String result = "";
    try {
           URL url = new URL("http://example.com/example.txt");       

        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line = null;

        while ((line = in.readLine()) != null) {
            //get lines
            result+=line;
        }
        in.close();


        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    return result;
}

 protected void onProgressUpdate() {
    //called when the background task makes any progress
 }

  protected void onPreExecute() {
     //called before doInBackground() is started
 }

@Override
 protected void onPostExecute(String result) {
     this.textViewToSet.setText(result); 
 }
  }

What you need to do next is to just call

new readtextfile().execute("http://example.com/example.txt");

and remove this line from onCreateView method

description.setText(descriptiontext);

这篇关于试图在android系统读取文本文件中的AsyncTask,但我得到空的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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