本文介绍了使用动作条Android蜂窝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
蜂窝错误android.os.NetworkOnMainThreadException后,我我的目标SDK设置为11.我用下面的codeS忽略它。它会影响我的应用程序?有人会建议什么解决这个问题。
StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建立()。StrictMode.setThreadPolicy(政策);@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.grid_layout);
。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建(); StrictMode.setThreadPolicy(政策); ArrayList的<&HashMap的LT;字符串,字符串>> songsList =新的ArrayList<&HashMap的LT;字符串,字符串>>(); XMLParser的解析器=新XMLParser的();
字符串XML = parser.getXmlFromUrl(URL); // URL从XML获得
文档的文档= parser.getDomElement(XML); //获得DOM元素 节点列表NL = doc.getElementsByTagName(KEY_SONG);
//遍历所有歌曲节点&LT循环;宋>
的for(int i = 0; I< nl.getLength();我++){
//创建新的HashMap
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
元素e =(元)nl.item(I)
//将每个子节点的HashMap键=>值
map.put(KEY_ID,parser.getValue(即KEY_ID));
map.put(KEY_TITLE,parser.getValue(即KEY_TITLE));
map.put(KEY_ARTIST,parser.getValue(即KEY_ARTIST));
map.put(KEY_DURATION,parser.getValue(即KEY_DURATION));
map.put(KEY_THUMB_URL,parser.getValue(即KEY_THUMB_URL)); //添加HashList到ArrayList的
songsList.add(地图);
}
解决方案
这不是很好的做法做主线程(UI线程)的网络访问。如果你的目标SDK是蜂窝或更高,它会给主线程网络exception.Try使用来避免这种情况。
code EX:
公共类yourclass延伸活动{
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.uoyrlayout);
//这是UI线程,你不应该在这里做的网络访问。
新LongRunning()执行(); //调用后台线程做网络接入
}
公共类LongRunning扩展的AsyncTask<太虚,太虚,太虚> {
在preExecute保护无效(){
// UI更新到这里之前,后台线程..
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){
//这不是UI线程
//你的网络存取权限
返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
//更新后,后台线程的UI
} }
}
Honeycomb error android.os.NetworkOnMainThreadException after i set my target sdk to 11. I used the below codes to ignore it. Will it effect my app? Can somebody suggest anything to overcome this problem.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
解决方案
It is not good practice to do network access on main thread(UI thread).if your target SDK is Honeycomb or higher, it will give network on main thread exception.Try using Asynac tasks to avoid this.
Code EX:
public class yourclass extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uoyrlayout);
//this is UI thread you should not do network access from here.
new LongRunning().execute();//call to background thread to do network access
}
public class LongRunning extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
//UI updating goes here before background thread..
}
@Override
protected Void doInBackground(Void... params) {
//This is not the UI thread
//do your network acces
return null;
}
@Override
protected void onPostExecute(Void result) {
//update your UI after background thread
}
}
}
这篇关于使用动作条Android蜂窝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!