我有以下类'MainActivity',它有2个方法downloadPageA和downloadPageB,一旦下载了内容,它们都会通过http连接检索内容,XMLfromString方法尝试将其解析为Document-Object。
问题: downloadPageA 按预期工作,但 downloadPageB 在解析时崩溃。
班级:
package com.example.testhttp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Worker().execute("");
}
public static Document XMLfromString(String v) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(v));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
public static String inputStreamToString(InputStream is) {
String s = "";
String line = "";
BufferedReader rd = null;
rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
s += line;
}
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
public static String downloadPageA(String url) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = null;
try {
response = httpclient.execute(httpGet);
return inputStreamToString(response.getEntity().getContent());
} catch (ClientProtocolException e) {
} catch (IOException e) {
} catch (IllegalStateException e1) {
} catch (TransformerFactoryConfigurationError e1) {
}
return null;
}
public static String downloadPageB(String url) {
HttpURLConnection connection = null;
URL serverAddress = null;
try {
serverAddress = new URL(url);
connection = null;
connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(10000);
connection.connect();
return inputStreamToString(connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
connection = null;
}
return "";
}
private class Worker extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... str) {
@SuppressWarnings("unused")
Document domA = XMLfromString(downloadPageA("http://en.wikipedia.com/wiki/Mario"));
@SuppressWarnings("unused")
Document domB = XMLfromString(downloadPageB("http://en.wikipedia.com/wiki/Mario"));
return "";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
堆栈跟踪:
03-13 19:39:06.352: W/System.err(28733): org.xml.sax.SAXParseException: Unexpected <! (position:START_DOCUMENT null@1:1 in java.io.StringReader@4166bfd0)
03-13 19:39:06.352: W/System.err(28733): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
03-13 19:39:06.352: W/System.err(28733): at com.example.testhttp.MainActivity.XMLfromString(MainActivity.java:64)
03-13 19:39:06.352: W/System.err(28733): at com.example.testhttp.MainActivity$Worker.doInBackground(MainActivity.java:119)
03-13 19:39:06.352: W/System.err(28733): at com.example.testhttp.MainActivity$Worker.doInBackground(MainActivity.java:1)
03-13 19:39:06.352: W/System.err(28733): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-13 19:39:06.352: W/System.err(28733): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 19:39:06.352: W/System.err(28733): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 19:39:06.352: W/System.err(28733): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-13 19:39:06.352: W/System.err(28733): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 19:39:06.352: W/System.err(28733): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 19:39:06.362: W/System.err(28733): at java.lang.Thread.run(Thread.java:856)
我想使用 HttpUrlConnection,它更快,并且 android 团队建议使用它而不是 HttpClient。对此错误的解释将非常好!
马里奥