Java Android Asynctask赢了';完不成
我正在为学校编写一个非常简单的游戏,但是需要使用asynctask。我正在使用ADT(Eclipse)。以下是相关代码:Java Android Asynctask赢了';完不成,java,android,eclipse,android-asynctask,adt,Java,Android,Eclipse,Android Asynctask,Adt,我正在为学校编写一个非常简单的游戏,但是需要使用asynctask。我正在使用ADT(Eclipse)。以下是相关代码: public void oklog(View view) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, URISyntaxException, ClientProtocolException, IOException, Illegal
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);
}
public void oklog(视图视图)抛出ParserConfiguration异常、TransformerConfiguration异常、TransformerException、URI语法异常、ClientProtocolException、IOException、IllegalStateException、SAXException{
新建log_async().execute();
}
字符串DocumentToString(文档文档)引发TransformerConfiguration异常,TransformerException{
TransformerFactory tf=TransformerFactory.newInstance();
变压器=tf.新变压器();
setOutputProperty(OutputKeys.OMIT_XML_声明,“yes”);
StringWriter编写器=新的StringWriter();
transform(新的DOMSource(doc)、新的StreamResult(writer));
字符串输出=writer.getBuffer().toString();/.replaceAll(“\n |\r”,”);
返回输出;
}
类log\u async扩展异步任务{
受保护的void onPreExecute(){
TextView tv1=(TextView)findViewById(R.id.textView3);
tv1.setText(“Pracuję…”);
}
@凌驾
受保护的字符串背景(字符串…无效){
TextView tv1=(TextView)findViewById(R.id.textView3);
tv1.setText(“哈哈”);
字符串a=“”;
试一试{
tv1.setText(“haha2”);
HttpClient=new DefaultHttpClient();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder docBuilder=null;
docBuilder=factory.newDocumentBuilder();
tv1.setText(“haha3”);
Document doc=docBuilder.newDocument();
元素环境=doc.createElements(“http://schemas.xmlsoap.org/soap/envelope/“,“肥皂:信封”);
元素体=doc.createElements(“http://schemas.xmlsoap.org/soap/envelope/“,“肥皂:主体”);
元素zaloguj=doc.createElements(“http://tempuri.org/“,“tc:Zaloguj”);
zaloguj.appendChild(doc.createElements(“http://tempuri.org/“,“tc:login”);
zaloguj.appendChild(doc.createElements(“http://tempuri.org/“,“tc:pass”);
EditText et1=(EditText)findViewById(R.id.editText1);
zaloguj.GetElementsbytagnames(“http://tempuri.org/.item(0.setTextContent(et1.getText().toString());
EditText et2=(EditText)findViewById(R.id.editText2);
zaloguj.GetElementsbytagnames(“http://tempuri.org/.item(0.setTextContent(et2.getText().toString());
tv1.setText(“haha4”);
儿童尸体(zaloguj);
环境儿童(身体);
附件儿童(环境)文件;
字符串s=null;
s=文档字符串(doc);
Log.i(“SOAP”,s);
StringEntity=null;
实体=新实体;
HttpPost=null;
post=新的HttpPost(新的URI(“http://www.kdkade.somee.com/oldschoolrpg_main.asmx"));
tv1.setText(“haha5”);
post.addHeader(“SOAPAction”http://tempuri.org/Zaloguj");
addHeader(“内容类型”,“text/xml;charset=utf-8”);
后设实体(实体);
HttpResponse响应=null;
响应=client.execute(post);
文档响应DC=null;
responseDoc=docBuilder.parse(response.getEntity().getContent());
tv1.setText(“haha6”);
s=文件字符串(响应DC);
Log.i(“响应”,s);
//TextView tv1=(TextView)findViewById(R.id.textView3);
字符串[]temp1=s.split(“”);
字符串[]temp2=temp1[1]。拆分(您正在尝试更新后台线程上的UI。您应该更新主UI线程上的UI。doInbackground()
在onPreExecute()之后立即在后台线程上调用
完成执行。此步骤用于执行可能需要很长时间的后台计算。使用runOnUiThread()
更新doInBackground()中的UI
onProgressUpdate(Progress…
在UI线程上调用。它可用于设置进度条动画或在文本字段中显示日志。您也可以使用它
但是我建议您在onPostExecute(param)
中更新UI。doInBackground()
的结果是在UI线程上调用onPostExecute(param)
的参数
您还可以在onCreate()
中初始化textview一次,然后使用相同的方法
您可以查看以下4个步骤下的主题@
如上所述,您正在从非UI线程(即运行log\u async
的后台线程)更新UI
在doInBackground()
方法中传递进度的一个选项是使用publishProgress()
,这将导致随后调用onProgressUpdate()
您还需要覆盖onProgressUpdate()
以适当地更新UI。您正在尝试在doInbackground.tv1.setText(“haha2”);中更新UI。您应该使用runonuithread来执行相同的操作,或者在onPostExecute(参数)中进行更新正如我在粘贴代码后所说,这只是暂时的,因为没有haha#textchanges它就无法工作。它总是来自OnPreExecute的“Pracuję”。这是因为OnPreExecute()在ui线程上调用,所以它会显示出来。检查日志和崩溃行。检查链接以获取更多信息@user2422841很高兴它对您有所帮助
runOnUiThread(new Runnable(){
@Override
public void run(){
// update ui here
}
});