Android:AsyncTask和阅读互联网数据

时间:2012-05-17 11:42:04

标签: android android-asynctask

我编写了一个从互联网上下载一些数据的代码。比我想把它变成asyncTask。之后,下载停止了工作。它似乎无法完成try {}部分所以跳过exept。

从主活动“Nekaj”我调用loadData()类,它扩展了AsyncData。从那里我在try命令中调用“oto”类。 “oto”类用于从Internet读取内容并返回字符串数组。当我直接从“Nekaj”类调用oto类时,这个工作。使用AsyncTask我做错了什么? 这是代码:

    public class Nekaj extends Activity {

TextView Tkolo, Tbroj1;
String[] brojevi_varijabla;
String privremena_varijabla = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bez_provjere_739);
    Tkolo = (TextView) findViewById(R.id.Xkolo);
    Tbroj1 = (TextView) findViewById(R.id.Xbroj1);

    /*
     * try { privremena_varijabla = test.kolo_739();
     * Tkolo.setText(privremena_varijabla); } catch (Exception e) { // TODO
     * Auto-generated catch block e.printStackTrace(); }
     */

    new loadData().execute();
}

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

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        Oto test = new Oto();

        try {
            brojevi_varijabla = test.brojevi_739();
            if (Integer.valueOf(brojevi_varijabla[0]) > 10) {
                Tbroj1.setText("" + brojevi_varijabla[0]);
            } else {
                Tbroj1.setText(" " + brojevi_varijabla[0]);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}

public class Oto {

    public String[] brojevi_739() throws Exception {

        int i = 0;
        int uvjet = 0;
        int varijabla = 0;
        char[] znak = { '>', '<' };
        BufferedReader in = null;
        String data[] = null;
        String provjera = "date-info";
        int[] polje = new int[2];

        try {
            HttpClient klijent = new DefaultHttpClient();
            URI webstranica = new URI(
                    "https://www.aaa.bb");
            HttpGet zahtjev = new HttpGet();
            zahtjev.setURI(webstranica);
            HttpResponse odgovor = klijent.execute(zahtjev);
            in = new BufferedReader(new InputStreamReader(odgovor
                    .getEntity().getContent()));
            StringBuffer brojevi = new StringBuffer("");
            String brojevi_string = null;
            String neki_string = null;
            String red = "";

            in.skip(21000);
            while ((red = in.readLine()) != null) {
                varijabla = red.indexOf(provjera);

                if (varijabla != -1) {

                    // 1. KOLO
                    if (uvjet == 0) { // onda sadrži taj
                                        // substring
                                        // !!!!
                        red = in.readLine(); // sada string red sadrži ono
                                                // što
                                                // želim, još moram samo to
                                                // izrezati!!
                        do {
                            if (i == 0) {
                                varijabla = red.indexOf(znak[i]);
                            }

                            else {
                                varijabla = red.indexOf(znak[i], polje[0]);
                            }

                            if (varijabla != -1) // ako taj znak postoji u
                                                    // stringu
                            {
                                if (i == 0) {
                                    polje[i] = varijabla + 1;
                                }

                                else {
                                    polje[i] = varijabla;
                                }
                                i++;
                            }

                        } while (i <= 1);

                        neki_string = red.substring(polje[0], polje[1]);
                        Tkolo.setText(neki_string);

                        provjera = "Dobitna kombinacija";
                        uvjet++;
                        continue;
                    }


                }
            }

            in.close();

            brojevi_string = brojevi.toString();

            data = brojevi_string.split("\n");

            return data;

        } finally {
            if (in != null) {
                try {
                    in.close();
                    return data;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}}

4 个答案:

答案 0 :(得分:1)

实际上,您正在尝试更新 AsyncTask doInBackGround()中的UI,因此不允许(doInBack ..在非UI线程中运行..),因此请更新UI AsyncTask onPostExecute()中的代码..

试试这个,让我知道发生了什么......

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

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        Oto test = new Oto();
        try {
            brojevi_varijabla = test.brojevi_739();       
            if(brojevi_varijabla != null)
            return brojevi_varijabla[0];
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

 @Override
    protected void onPostExecute(String result)
    {
        if(result != null)
          {
            if (Integer.valueOf(result) > 10) {
                Tbroj1.setText("" + result;
            } else {
                Tbroj1.setText(" " + result);
            }
          }
     }
}

答案 1 :(得分:1)

你做错了是doInBackground()方法中的Tbroj1.setText()。您需要做的是使用onPostExecute方法在UI上发布您的数据:

public class loadData extends AsyncTask<String, Integer, Boolean> {
 protected Long doInBackground(String... arg0) {
     Oto test = new Oto();
     Boolean result = false;

    try {
        brojevi_varijabla = test.brojevi_739();
        result = true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
 }


 protected void onPostExecute(Boolean result) {
     if(result){
        if (Integer.valueOf(brojevi_varijabla[0]) > 10) {
            Tbroj1.setText("" + brojevi_varijabla[0]);
        } else {
            Tbroj1.setText(" " + brojevi_varijabla[0]);
        }
     }

 }
}

答案 2 :(得分:0)

使用onPostExecute(Void result1){}

捕获结果并执行那里所需的操作

答案 3 :(得分:0)

您无法直接在非UI(后台)线程上操作UI元素,这是doInBackground()始终运行的位置。使用AsyncTask的常用方法是获取doInBackground()中的数据,将其作为值返回,然后在onPostExecute()中处理UI更改。例如:

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

@Override
protected String doInBackground(String... arg0) {
    Oto test = new Oto();

    try {
        brojevi_varijabla = test.brojevi_739();
        if (Integer.valueOf(brojevi_varijabla[0]) > 10) {
            return "" + brojevi_varijabla[0];
        } else {
            return " " + brojevi_varijabla[0];
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    if (result != null) Tbroj1.setText(result);
}

}