![](/img/trans.png)
[英]AsyncTask doesn't finish after running doInBackground Android
[英]Android Asynctask won't finish
我正在為學校寫一個非常簡單的游戲,但是需要使用asynctask。 我正在使用ADT(Eclipse)。 這是相關代碼:
public void oklog(View view) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, URISyntaxException, ClientProtocolException, IOException, IllegalStateException, SAXException {
new log_async().execute();
}
String DocumentToString(Document doc) throws TransformerConfigurationException, TransformerException{
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString();//.replaceAll("\n|\r", "");
return output;
}
class log_async extends AsyncTask<String, Void, String> {
protected void onPreExecute(){
TextView tv1 = (TextView) findViewById(R.id.textView3);
tv1.setText("Pracuję...");
}
@Override
protected String doInBackground(String...voids ) {
TextView tv1 = (TextView) findViewById(R.id.textView3);
tv1.setText("haha");
String a = "";
try {
tv1.setText("haha2");
HttpClient client = new DefaultHttpClient();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder docBuilder = null;
docBuilder = factory.newDocumentBuilder();
tv1.setText("haha3");
Document doc = docBuilder.newDocument();
Element env = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soap:Envelope");
Element body = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soap:Body");
Element zaloguj = doc.createElementNS("http://tempuri.org/", "tc:Zaloguj");
zaloguj.appendChild(doc.createElementNS("http://tempuri.org/", "tc:login"));
zaloguj.appendChild(doc.createElementNS("http://tempuri.org/", "tc:pass"));
EditText et1 = (EditText)findViewById(R.id.editText1);
zaloguj.getElementsByTagNameNS("http://tempuri.org/", "login").item(0).setTextContent(et1.getText().toString());
EditText et2 = (EditText)findViewById(R.id.editText2);
zaloguj.getElementsByTagNameNS("http://tempuri.org/", "pass").item(0).setTextContent(et2.getText().toString());
tv1.setText("haha4");
body.appendChild(zaloguj);
env.appendChild(body);
doc.appendChild(env);
String s = null;
s = DocumentToString(doc);
Log.i("SOAP", s);
StringEntity entity = null;
entity = new StringEntity(s);
HttpPost post = null;
post = new HttpPost(new URI("http://www.kdkade.somee.com/oldschoolrpg_main.asmx"));
tv1.setText("haha5");
post.addHeader("SOAPAction", "http://tempuri.org/Zaloguj");
post.addHeader("Content-Type", "text/xml; charset=utf-8");
post.setEntity(entity);
HttpResponse response = null;
response = client.execute(post);
Document responseDoc = null;
responseDoc = docBuilder.parse(response.getEntity().getContent());
tv1.setText("haha6");
s = DocumentToString(responseDoc);
Log.i("RESPONSE", s);
//TextView tv1 = (TextView) findViewById(R.id.textView3);
String[] temp1 = s.split("<ZalogujResult>");
String[] temp2 = temp1[1].split("</");
tv1.setText(temp2[0]);
a = temp2[0];
//tv1.setText(responseDoc.getElementsByTagNameNS("http://tempuri.org/","ZalogujResult").toString());
//tv1.setText(s);
//return null;
//return null;
}
catch (Exception e){
tv1.setText(e.getMessage());
}
tv1.setText("wtf");
return a;
}
//protected void onProgressUpdate(Integer... progress) {
//setProgressPercent(progress[0]);
//}
@Override
protected void onPostExecute(String result) {
TextView tv1 = (TextView) findViewById(R.id.textView3);
tv1.setText(result);
}
doinbackground里面的代碼之前是oklog方法,它運行正常。 正如你所看到的,我在這里和那里放了一些TextView文本變換,在模擬器上看到它有多遠,它有時只是“Pracuję...”(OnPreExecute中的那個),有時它甚至會變成“haha5”和有時應用程序崩潰(似乎相當隨機)。 不幸的是,我在這里失去了什么錯。 誰能告訴我哪里出錯或是模擬器問題?
您正在嘗試在后台線程上更新UI。 你應該在主ui線程上更新ui。 doInbackground()
onPreExecute()
完成執行后立即在后台線程上調用onPreExecute()
。 此步驟用於執行可能需要很長時間的后台計算。 使用runOnUiThread()
在更新UI doInBackground()
在UI線程上調用onProgressUpdate(Progress...)
。 它可用於為進度條設置動畫或在文本字段中顯示日志。 你也可以使用它。
但我建議你在onPostExecute(param)
更新UI。 doInBackground()
的結果是onPostExecute(param)
。 在UI線程上調用onPostExecute(param)
。
您也可以在onCreate()
初始化textview一次並使用相同的文本視圖。
你可以查看4個步驟下的主題@ http://developer.android.com/reference/android/os/AsyncTask.html
runOnUiThread(new Runnable(){
@Override
public void run(){
// update ui here
}
});
如上面的答案所述,您正在從非UI線程(即運行log_async
的后台線程)更新UI。
在doInBackground()
方法中傳遞進度的選項是使用publishProgress()
,這將導致隨后調用onProgressUpdate()
。
您還需要覆蓋onProgressUpdate()
以適當地更新UI。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.