2

我写了一个代码来从互联网上下载一些数据。比我想把它放到 asyncTask 中。之后下载停止工作。看起来它无法完成 try{} 部分,所以跳到 exeption。

从主要活动“Nekaj”我调用 loadData() 类,它扩展了 AsyncData。从那里我在 try 命令中调用“oto”类。“oto”类用于从互联网读取内容并返回字符串数组。当我直接从“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

4 回答 4

1

What you are doing wrong is Tbroj1.setText() inside the doInBackground() method. What you have to do is to use the onPostExecute method to post your data on the 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]);
        }
     }

 }
}
于 2012-05-17T12:00:04.237 回答
1

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

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

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);
            }
          }
     }
}
于 2012-05-17T11:52:40.510 回答
0

您不能直接在doInBackground()始终运行的非 UI(后台)线程上操作 UI 元素。通常的使用方式AsyncTask是获取 中的数据doInBackground(),将其作为值返回,然后处理中的 UI 变化onPostExecute()。例如:

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);
}

}

于 2012-05-17T11:56:15.387 回答
0

use onPostExecute(Void result1) {}

to catch the result and perform the action required over there

于 2012-05-17T11:51:19.083 回答