我正在为学校编写一个非常简单的游戏,但需要使用 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" 和有时应用程序崩溃(似乎相当随机)。不幸的是,我失去了这里的问题。谁能告诉我错误在哪里还是模拟器问题?