我正在尝试调用外部xml,我有一些错误。
错误
06-02 04:32:21.085 1658-1658 / com.golftipp E / Ideas4All:错误 android.os.NetworkOnMainThreadException 在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) at java.net.InetAddress.lookupHostByName(InetAddress.java:385) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByName(InetAddress.java:214) 在com.android.okhttp.internal.Dns $ 1.getAllByName(Dns.java:28) 在com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) 在com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) 在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) 在com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) 在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) 在com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) 在com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 在com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179) 在java.net.URL.openStream(URL.java:470) 在com.golftipp.IntroduceCodigo.loadCategories(IntroduceCodigo.java:111) 在com.golftipp.IntroduceCodigo.irFormularioReserva(IntroduceCodigo.java:67) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在android.view.View $ 1.onClick(View.java:3818) 在android.view.View.performClick(View.java:4438) 在android.view.View $ PerformClick.run(View.java:18422) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在dalvik.system.NativeStart.main(本地方法)
代码
private Vector<datos_codigos> categories;
private String categories_url = "http://www.url.com/beta.xml";
public void loadCategories() {
try {
// Url del archivo XML
URL url = new URL(categories_url);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
// Utilizamos nuestro propio parseador (CategoryHandler)
xmlSax myExampleHandler = new xmlSax();
xr.setContentHandler(myExampleHandler);
InputSource is = new InputSource(url.openStream());
// Le indicamos la codificación para evitar errores
is.setEncoding("UTF-8");
xr.parse(is);
// Asignamos al vector categories los datos parseados
categories = myExampleHandler.getParsedData();
System.out.println(myExampleHandler.getParsedData());
} catch (Exception e) {
// Ha ocurrido algún error
Log.e("Ideas4All", "Error", e);
}
}
答案 0 :(得分:0)
为此,您必须在loadCategories()
方法
doInBackground
因为在4.0之后它不允许在你的主ui线程上进行网络呼叫。
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
loadCategories();
return null;
}
}.execute();
答案 1 :(得分:0)
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);