У меня возникла проблема с загрузкой XML-файла из Интернета в Android. Я написал код, и он работает на эмуляторе Android 2.3. Тем не менее, он не работает на эмуляторе Android 4.03, и он не работает на реальном устройстве Android 4. Вот мой код:Исключение при загрузке XML-файла из Интернета на Android 4 (но он работает на Android 2.3)
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
У меня есть ошибка в строке:
HttpResponse httpResponse = httpClient.execute(httpPost);
Вот лог из LogCat:
08-15 16:59:32.436: E/AndroidRuntime(623): FATAL EXCEPTION: main
08-15 16:59:32.436: E/AndroidRuntime(623): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testy/com.example.testy.MainActivity}: android.os.NetworkOnMainThreadException
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.os.Looper.loop(Looper.java:137)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-15 16:59:32.436: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 16:59:32.436: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511)
08-15 16:59:32.436: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-15 16:59:32.436: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-15 16:59:32.436: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
08-15 16:59:32.436: E/AndroidRuntime(623): Caused by: android.os.NetworkOnMainThreadException
08-15 16:59:32.436: E/AndroidRuntime(623): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-15 16:59:32.436: E/AndroidRuntime(623): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-15 16:59:32.436: E/AndroidRuntime(623): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-15 16:59:32.436: E/AndroidRuntime(623): at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-15 16:59:32.436: E/AndroidRuntime(623): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-15 16:59:32.436: E/AndroidRuntime(623): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-15 16:59:32.436: E/AndroidRuntime(623): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-15 16:59:32.436: E/AndroidRuntime(623): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-15 16:59:32.436: E/AndroidRuntime(623): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-15 16:59:32.436: E/AndroidRuntime(623): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-15 16:59:32.436: E/AndroidRuntime(623): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-15 16:59:32.436: E/AndroidRuntime(623): at com.example.testy.XMLParser.getXmlFromUrl(XMLParser.java:54)
08-15 16:59:32.436: E/AndroidRuntime(623): at com.example.testy.MainActivity.onCreate(MainActivity.java:34)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.Activity.performCreate(Activity.java:4465)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-15 16:59:32.436: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-15 16:59:32.436: E/AndroidRuntime(623): ... 11 more
Что я могу сделать с этим?