gpt4 book ai didi

Android:AsyncTask 和读取互联网数据

转载 作者:行者123 更新时间:2023-11-29 00:39:07 25 4
gpt4 key购买 nike

我写了一个代码来从互联网上下载一些数据。比我想把它放到 asyncTask 中。然后下载停止工作。看起来它无法完成 try{} 部分,因此跳到异常(exception)。

在主要 Activity “Nekaj”中,我调用了扩展 AsyncData 的 loadData() 类。从那里我在 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();
}
}
}
}


}}

最佳答案

你做错的是 Tbroj1.setText() 里面的 doInBackground() 方法。你要做的是使用 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]);
}
}

}
}

关于Android:AsyncTask 和读取互联网数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10635073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com