XML动态加载如何在直播视频网站源码中实现
以下是直播视频网站源码实现XML动态加载的相关代码
```cpp public static View loadUrlXml(Context context,String url){ View parent = null; //判断是否主线程 if (Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId()) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } try { byte[] result = null; HttpURLConnection urlcon = (HttpURLConnection) new URL(url).openConnection(); urlcon.connect(); InputStream in = urlcon.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int i; while ((i = in.read(buf)) != -1) { bos.write(buf, 0, i); } in.close(); result = bos.toByteArray(); bos.close(); urlcon.disconnect(); Class<?> xmlBlock= Class.forName("android.content.res.XmlBlock"); Constructor cobj= xmlBlock.getConstructor(byte[].class); cobj.setAccessible(true); Object obj= cobj.newInstance(result); Method m= xmlBlock.getDeclaredMethod("newParser"); m.setAccessible(true); XmlResourceParser parser= (XmlResourceParser) m.invoke(obj); parent = LayoutInflater.from(context).inflate(parser, null); } catch (Exception e) { e.printStackTrace(); } return parent; } ```
以上就是以下是直播视频网站源码实现XML动态加载的相关代码, 更多内容欢迎关注之后的文章