次のクラス「MainActivity」があり、downloadPageA と downloadPageB の 2 つのメソッドがあり、コンテンツがダウンロードされると、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 の代わりに使用することをお勧めします。このエラーの説明はとてもいいです!
マリオ