应用程序在做它的主线程的工作太多了? [英] Application is doing too much work on its main thread?
问题描述
我有我的Android应用程序巨大的问题。在应用中,我获取数据从JSON流每三十秒更新列表视图 - 如果有新的数据,我只更新自定义适配器。如果我叫JSONParse /更新任务一次,一切正常,但是当我把这个方法的计时器任务里面,我的应用程序运行几乎没有,到最后10秒后倒闭。如果有什么办法可以帮我,我会感激之余。我真的很抱歉这里存在这么多的code。最重要的是,要注意callAsynchonousTask()方法。
下面是大多数应用程序的工作正在做的类:
公共类LiveStreamFragment扩展片段{公共WXYCMediaPlayer媒体播放器;ListView控件列表;
TextView的歌曲;
TextView的艺术家;公众的ArrayList<&HashMap的LT;字符串,字符串>> oslist =新的ArrayList<&HashMap的LT;字符串,字符串>>();私人静态字符串wxycUrl =http://www.wxyc.info/playlists/recentEntries?v=2;
私人静态字符串streamURL =http://152.2.204.90:8000/wxyc.mp3;私有静态最后弦乐TAG_PLAYCUTS =playcuts;
私有静态最后弦乐TAG_SONG =SONGTITLE;
私有静态最后弦乐TAG_ARTIST =ARTISTNAME;
私有静态最后弦乐TAG_ALBUM =releaseTitle;
私有静态最后弦乐TAG_TALKSETS =talksets;
私有静态最后弦乐TAG_CHRONID =chronOrderID;
私有静态最后弦乐TAG_BREAKPOINTS =断点;
私有静态最后弦乐TAG_HOUR =小时;
私有静态最后弦乐TAG_LAYOUT =layoutType;私人SwipeRefreshLayout swipeLayout;
私人按钮update_button;私人JSONArray playcuts = NULL;
私人JSONArray talksets = NULL;
私人JSONArray断点= NULL;私人Playcut [] playcutArr;
私人Talkset [] talksetArr;
私人断点[] breakpointArr;公共查看rootView;布尔华美通=真;
布尔buttonActivated;LiveAdapter适配器;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
super.onCreate(savedInstanceState); rootView = inflater.inflate(R.layout.stream_fragment,集装箱,FALSE); 名单=(ListView控件)rootView.findViewById(R.id.list);
适配器=新LiveAdapter(LiveStreamFragment.this.getActivity(),oslist,LiveStreamFragment.this,清单);
list.setAdapter(适配器); buttonActivated = FALSE; 新JSONParse()执行();
this.callAsynchronousTask(); update_button =(按钮)rootView.findViewById(R.id.update_button);
update_button.setText(新的轨道!);
update_button.setGravity(Gravity.CENTER_HORIZONTAL);
update_button.setVisibility(View.GONE);
update_button.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
adapter.updateDataList(oslist);
update_button.setVisibility(View.GONE);
buttonActivated = FALSE;
}
}); list.setOnScrollListener(新AbsListView.OnScrollListener(){
@覆盖
公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){
如果(scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
如果(buttonActivated){
update_button.setVisibility(View.VISIBLE);
}
}
} @覆盖
公共无效onScroll(AbsListView观点,诠释firstVisibleItem,诠释visibleItemCount,诠释totalItemCount){
如果(buttonActivated){
update_button.setVisibility(View.GONE);
}
}
}); 返回rootView;
}公共无效addHeartData(HashMap的<字符串,字符串> heartMap){
Bus总线=新总线();
。BusProvider.getInstance()后(heartMap);
}/ ********调用JSON供稿每隔30秒更新******** /
公共无效callAsynchronousTask(){
最后的处理程序处理程序=新的处理程序();
定时器定时器=新的Timer();
TimerTask的doAsynchronousTask =新的TimerTask(){
@覆盖
公共无效的run(){
handler.post(新的Runnable(){
公共无效的run(){
尝试{
新JSONParse()执行(); }赶上(例外五){
// TODO自动生成catch块
}
}
});
}
};
timer.schedule(doAsynchronousTask,0,30000); //每50000毫秒执行
}/ ******** JSON解析和分类类******** /
公共类JSONParse扩展的AsyncTask<字符串,字符串,JSONObject的> {
私人ProgressDialog pDialog; 私人字符串chronIDCheck; @覆盖
在preExecute保护无效(){
super.on preExecute(); oslist =新的ArrayList<&HashMap的LT;字符串,字符串>>(); / * DELAY此任务闪屏时间* /
// MEDIAPLAYER =新WXYCMediaPlayer(streamURL,this.getActivity()); // HashMap的<字符串,字符串> streamMap =新的HashMap<字符串,字符串>(); //这个添加到媒体播放器的方法。
//streamMap.put(TAG_LAYOUT,直播);
//oslist.add(streamMap); / *名单=(ListView控件)rootView.findViewById(R.id.list);
适配器=新LiveAdapter(LiveStreamFragment.this.getActivity(),oslist,LiveStreamFragment.this,清单);
list.setAdapter(适配器); * /
} @覆盖
受保护的JSONObject doInBackground(字符串参数... args){
JSONParser jParser =新JSONParser();
// URL从JSON入门
JSONObject的JSON = jParser.getJSONFromUrl(wxycUrl); 返回JSON;
} @覆盖
保护无效onPostExecute(JSON的JSONObject){
尝试{ playcuts = json.getJSONArray(TAG_PLAYCUTS);
talksets = json.getJSONArray(TAG_TALKSETS);
断点= json.getJSONArray(TAG_BREAKPOINTS); playcutArr =新Playcut [playcuts.length()];
talksetArr =新Talkset [talksets.length()];
breakpointArr =新断点[breakpoints.length()]; 的for(int i = 0; I< playcuts.length();我++){
JSONObject的playcut = playcuts.getJSONObject(I) playcutArr [I] =新Playcut(
playcut.getString(TAG_SONG)
playcut.getString(TAG_ARTIST)
playcut.getString(TAG_ALBUM)
playcut.getInt(TAG_CHRONID));
} 对于(INT J = 0; J< talksets.length(); J ++){
JSONObject的talkset = talksets.getJSONObject(J); talksetArr [J] =新Talkset(
talkset.getInt(TAG_CHRONID)); } 对于(INT K = 0; K< breakpoints.length(); K ++){
JSONObject的断点= breakpoints.getJSONObject(K); breakpointArr [K] =新断点(
breakpoint.getInt(TAG_CHRONID)
breakpoint.getLong(TAG_HOUR)
);
} }赶上(JSONException E){
e.printStackTrace();
} INT playcutIndex = 0;
INT talksetIndex = 0;
INT breakpointIndex = 0;
INT minID;
INT I = 0; / ********算法由他们的年代ID ********巩固playcuts,断点和成一个ArrayList中talksets / 而(ⅰ小于30){ HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); minID = Math.max(
playcutArr [playcutIndex] .chronID,(INT)Math.max(
talksetArr [talksetIndex] .chronID,breakpointArr [breakpointIndex] .chronID
)
); 如果(minID == playcutArr [playcutIndex] .chronID){ map.put(TAG_SONG,playcutArr [playcutIndex]。宋);
map.put(TAG_ARTIST,playcutArr [playcutIndex] .artist);
map.put(TAG_ALBUM,playcutArr [playcutIndex] .album);
map.put(TAG_LAYOUTPlaycut);
map.put(TAG_CHRONID,+ playcutArr [playcutIndex] .chronID); StringBuilder的StringBuilder的=新的StringBuilder(http://ws.audioscrobbler.com/2.0/);
stringBuilder.append(法= album.getinfo?);
stringBuilder.append(与& API_KEY =);
stringBuilder.append(2ead17554acf667f27cf7dfd4c368f15);
字符串albumURL = NULL; 尝试{
stringBuilder.append(与&艺术家=+ URLEn coder.en code(map.get(TAG_ARTIST),UTF-8));
stringBuilder.append(与&专辑=+ URLEn coder.en code(map.get(TAG_ALBUM),UTF-8));
albumURL =新RetrieveAlbumArtUrlTask()执行(stringBuilder.toString())获得()。 }赶上(UnsupportedEncodingException五){
albumURL = NULL;
}赶上(InterruptedException的E){
albumURL = NULL;
}赶上(为ExecutionException E){
albumURL = NULL;
}赶上(抛出:IllegalArgumentException五){
albumURL = NULL;
} map.put(albumArtUrl,albumURL); playcutIndex = playcutIndex + 1;
} 如果(minID == talksetArr [talksetIndex] .chronID){ map.put(TAG_SONGTalkset);
map.put(TAG_ARTIST,NULL);
map.put(TAG_LAYOUTTalkset);
map.put(TAG_CHRONID,+ talksetArr [talksetIndex] .chronID); talksetIndex = talksetIndex + 1;
} 如果(minID == breakpointArr [breakpointIndex] .chronID){ map.put(TAG_SONG断点);
map.put(TAG_ARTIST,NULL);
map.put(TAG_LAYOUT断点);
map.put(TAG_HOUR,+ breakpointArr [breakpointIndex]·小时);
map.put(TAG_CHRONID,+ breakpointArr [breakpointIndex] .chronID); breakpointIndex = breakpointIndex + 1;
} map.put(点击,假); oslist.add(地图); chronIDCheck = oslist.get(0)获得(TAG_CHRONID);
我++; } / *如果这是第一个JSON解析,我们实例适配器,否则我们刚刚更新* /
如果(新华美通){ 名单=(ListView控件)rootView.findViewById(R.id.list);
适配器=新LiveAdapter(LiveStreamFragment.this.getActivity(),oslist,LiveStreamFragment.this,清单);
list.setAdapter(适配器); 新华美通= FALSE; }其他{ 如果(!adapter.chronIdCheck()。等于(oslist.get(0)获得(TAG_CHRONID))){
//adapter.updateDataList(oslist);
update_button.setVisibility(View.VISIBLE);
buttonActivated = TRUE; } }
} }}
下面是我的新doInBackground()code:
@覆盖
保护无效doInBackground(字符串参数... args){
jParser =新JSONParser();
JSON = jParser.getJSONFromUrl(wxycUrl);
Log.v(TEST,背景); 尝试{ playcuts = json.getJSONArray(TAG_PLAYCUTS);
talksets = json.getJSONArray(TAG_TALKSETS);
断点= json.getJSONArray(TAG_BREAKPOINTS); playcutArr =新Playcut [playcuts.length()];
talksetArr =新Talkset [talksets.length()];
breakpointArr =新断点[breakpoints.length()]; 的for(int i = 0; I< playcuts.length();我++){
JSONObject的playcut = playcuts.getJSONObject(I) playcutArr [I] =新Playcut(
playcut.getString(TAG_SONG)
playcut.getString(TAG_ARTIST)
playcut.getString(TAG_ALBUM)
playcut.getInt(TAG_CHRONID));
} 对于(INT J = 0; J< talksets.length(); J ++){
JSONObject的talkset = talksets.getJSONObject(J); talksetArr [J] =新Talkset(
talkset.getInt(TAG_CHRONID)); } 对于(INT K = 0; K< breakpoints.length(); K ++){
JSONObject的断点= breakpoints.getJSONObject(K); breakpointArr [K] =新断点(
breakpoint.getInt(TAG_CHRONID)
breakpoint.getLong(TAG_HOUR)
);
} }赶上(JSONException E){
e.printStackTrace();
} INT playcutIndex = 0;
INT talksetIndex = 0;
INT breakpointIndex = 0;
INT minID;
INT I = 0; / ********算法由他们的年代ID ********巩固playcuts,断点和成一个ArrayList中talksets / 而(ⅰ小于30){ HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); minID = Math.max(
playcutArr [playcutIndex] .chronID,(INT)Math.max(
talksetArr [talksetIndex] .chronID,breakpointArr [breakpointIndex] .chronID
)
); 如果(minID == playcutArr [playcutIndex] .chronID){ map.put(TAG_SONG,playcutArr [playcutIndex]。宋);
map.put(TAG_ARTIST,playcutArr [playcutIndex] .artist);
map.put(TAG_ALBUM,playcutArr [playcutIndex] .album);
map.put(TAG_LAYOUTPlaycut);
map.put(TAG_CHRONID,+ playcutArr [playcutIndex] .chronID); StringBuilder的StringBuilder的=新的StringBuilder(http://ws.audioscrobbler.com/2.0/);
stringBuilder.append(法= album.getinfo?);
stringBuilder.append(与& API_KEY =);
stringBuilder.append(2ead17554acf667f27cf7dfd4c368f15);
字符串albumURL = NULL; 尝试{
stringBuilder.append(与&艺术家=+ URLEn coder.en code(map.get(TAG_ARTIST),UTF-8));
stringBuilder.append(与&专辑=+ URLEn coder.en code(map.get(TAG_ALBUM),UTF-8));
albumURL =新RetrieveAlbumArtUrlTask()执行(stringBuilder.toString())获得()。 }赶上(UnsupportedEncodingException五){
albumURL = NULL;
}赶上(InterruptedException的E){
albumURL = NULL;
}赶上(为ExecutionException E){
albumURL = NULL;
}赶上(抛出:IllegalArgumentException五){
albumURL = NULL;
} map.put(albumArtUrl,albumURL); playcutIndex = playcutIndex + 1;
} 如果(minID == talksetArr [talksetIndex] .chronID){ map.put(TAG_SONGTalkset);
map.put(TAG_ARTIST,NULL);
map.put(TAG_LAYOUTTalkset);
map.put(TAG_CHRONID,+ talksetArr [talksetIndex] .chronID); talksetIndex = talksetIndex + 1;
} 如果(minID == breakpointArr [breakpointIndex] .chronID){ map.put(TAG_SONG断点);
map.put(TAG_ARTIST,NULL);
map.put(TAG_LAYOUT断点);
map.put(TAG_HOUR,+ breakpointArr [breakpointIndex]·小时);
map.put(TAG_CHRONID,+ breakpointArr [breakpointIndex] .chronID); breakpointIndex = breakpointIndex + 1;
} map.put(点击,假); oslist.add(地图); chronIDCheck = oslist.get(0)获得(TAG_CHRONID);
我++; } Log.v(测试,背景2); 返回null;
} @覆盖
保护无效onPostExecute(无效参数){
Log.v(TEST,POST); / *如果这是第一个JSON解析,我们实例适配器,否则我们刚刚更新* /
如果(新华美通){ 名单=(ListView控件)rootView.findViewById(R.id.list);
适配器=新LiveAdapter(LiveStreamFragment.this.getActivity(),oslist,LiveStreamFragment.this,清单);
list.setAdapter(适配器); 新华美通= FALSE; }其他{ 如果(!adapter.chronIdCheck()。等于(oslist.get(0)获得(TAG_CHRONID))){
//adapter.updateDataList(oslist);
update_button.setVisibility(View.VISIBLE);
buttonActivated = TRUE; } } }}
该方法 onPostExecute
被称为UI线程,和你正在做的很多东西在里面。尽量保持code从如果(新华美通)
在 onPostExecute
,因为这是你需要访问用户界面。上面的code的其余部分可以移动到 doInBackground
,它在后台线程调用。
从文档:
doInBackground(参数...),调用在后台线程
后preExecute立即()执行完毕。
onPostExecute(结果),背景后,在UI线程调用
计算完成。
块引用>I am having huge problems with my android application. In the app, I fetch data every thirty seconds from a JSON stream to update a listview--i only update the custom adapter if there is new data. If I call the JSONParse/update tasks once, everything works fine, though when I put this method inside a timer task, my application barely runs and ends up closing down after 10 seconds. If there is any way you can help me, i would be more than grateful. I am really sorry there is so much code here. Most importantly, pay attention to the callAsynchonousTask() method.
Here is the class where most the application work is being done:
public class LiveStreamFragment extends Fragment{ public WXYCMediaPlayer mediaPlayer; ListView list; TextView song; TextView artist; public ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>(); private static String wxycUrl = "http://www.wxyc.info/playlists/recentEntries?v=2"; private static String streamURL = "http://152.2.204.90:8000/wxyc.mp3"; private static final String TAG_PLAYCUTS = "playcuts"; private static final String TAG_SONG = "songTitle"; private static final String TAG_ARTIST = "artistName"; private static final String TAG_ALBUM = "releaseTitle"; private static final String TAG_TALKSETS = "talksets"; private static final String TAG_CHRONID = "chronOrderID"; private static final String TAG_BREAKPOINTS = "breakpoints"; private static final String TAG_HOUR = "hour"; private static final String TAG_LAYOUT = "layoutType"; private SwipeRefreshLayout swipeLayout; private Button update_button; private JSONArray playcuts = null; private JSONArray talksets = null; private JSONArray breakpoints = null; private Playcut[] playcutArr; private Talkset[] talksetArr; private Breakpoint[] breakpointArr; public View rootView; boolean firstCall = true; boolean buttonActivated; LiveAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); rootView = inflater.inflate(R.layout.stream_fragment, container, false); list = (ListView) rootView.findViewById(R.id.list); adapter = new LiveAdapter(LiveStreamFragment.this.getActivity(), oslist, LiveStreamFragment.this, list); list.setAdapter(adapter); buttonActivated = false; new JSONParse().execute(); this.callAsynchronousTask(); update_button = (Button) rootView.findViewById(R.id.update_button); update_button.setText("New Tracks!"); update_button.setGravity(Gravity.CENTER_HORIZONTAL); update_button.setVisibility(View.GONE); update_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.updateDataList(oslist); update_button.setVisibility(View.GONE); buttonActivated = false; } }); list.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { if(buttonActivated) { update_button.setVisibility(View.VISIBLE); } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(buttonActivated) { update_button.setVisibility(View.GONE); } } }); return rootView; } public void addHeartData(HashMap<String, String> heartMap){ Bus bus = new Bus(); BusProvider.getInstance().post(heartMap); } /******** Calling the JSON Feed to update every 30 seconds ********/ public void callAsynchronousTask() { final Handler handler = new Handler(); Timer timer = new Timer(); TimerTask doAsynchronousTask = new TimerTask() { @Override public void run() { handler.post(new Runnable() { public void run() { try { new JSONParse().execute(); } catch (Exception e) { // TODO Auto-generated catch block } } }); } }; timer.schedule(doAsynchronousTask, 0, 30000); //execute in every 50000 ms } /******** JSON Parsing and sorting class ********/ public class JSONParse extends AsyncTask<String, String, JSONObject> { private ProgressDialog pDialog; private String chronIDCheck; @Override protected void onPreExecute() { super.onPreExecute(); oslist = new ArrayList<HashMap<String, String>>(); /*DELAY THIS TASK FOR THE SPLASH SCREEN TIME*/ //mediaPlayer = new WXYCMediaPlayer(streamURL, this.getActivity()); //HashMap<String, String> streamMap = new HashMap<String, String>(); //Add this to the media player method. //streamMap.put(TAG_LAYOUT, "LiveStream"); //oslist.add(streamMap); /*list = (ListView) rootView.findViewById(R.id.list); adapter = new LiveAdapter(LiveStreamFragment.this.getActivity(), oslist, LiveStreamFragment.this, list); list.setAdapter(adapter);*/ } @Override protected JSONObject doInBackground(String... args) { JSONParser jParser = new JSONParser(); // Getting JSON from URL JSONObject json = jParser.getJSONFromUrl(wxycUrl); return json; } @Override protected void onPostExecute(JSONObject json) { try { playcuts = json.getJSONArray(TAG_PLAYCUTS); talksets = json.getJSONArray(TAG_TALKSETS); breakpoints = json.getJSONArray(TAG_BREAKPOINTS); playcutArr = new Playcut[playcuts.length()]; talksetArr = new Talkset[talksets.length()]; breakpointArr = new Breakpoint[breakpoints.length()]; for(int i = 0; i < playcuts.length(); i++){ JSONObject playcut = playcuts.getJSONObject(i); playcutArr[i] = new Playcut( playcut.getString(TAG_SONG), playcut.getString(TAG_ARTIST), playcut.getString(TAG_ALBUM), playcut.getInt(TAG_CHRONID)); } for(int j = 0; j < talksets.length(); j++){ JSONObject talkset = talksets.getJSONObject(j); talksetArr[j] = new Talkset( talkset.getInt(TAG_CHRONID)); } for(int k = 0; k < breakpoints.length(); k++){ JSONObject breakpoint = breakpoints.getJSONObject(k); breakpointArr[k] = new Breakpoint( breakpoint.getInt(TAG_CHRONID), breakpoint.getLong(TAG_HOUR) ); } } catch (JSONException e) { e.printStackTrace(); } int playcutIndex = 0; int talksetIndex = 0; int breakpointIndex = 0; int minID; int i = 0; /******** Algorithm to consolidate playcuts, breakpoints, and talksets into one arraylist by their chronological ID ********/ while(i < 30){ HashMap<String, String> map = new HashMap<String, String>(); minID = Math.max( playcutArr[playcutIndex].chronID, (int) Math.max( talksetArr[talksetIndex].chronID, breakpointArr[breakpointIndex].chronID ) ); if(minID == playcutArr[playcutIndex].chronID) { map.put(TAG_SONG, playcutArr[playcutIndex].song); map.put(TAG_ARTIST, playcutArr[playcutIndex].artist); map.put(TAG_ALBUM, playcutArr[playcutIndex].album); map.put(TAG_LAYOUT, "Playcut"); map.put(TAG_CHRONID,""+playcutArr[playcutIndex].chronID); StringBuilder stringBuilder = new StringBuilder("http://ws.audioscrobbler.com/2.0/"); stringBuilder.append("?method=album.getinfo"); stringBuilder.append("&api_key="); stringBuilder.append("2ead17554acf667f27cf7dfd4c368f15"); String albumURL = null; try { stringBuilder.append("&artist=" + URLEncoder.encode(map.get(TAG_ARTIST), "UTF-8")); stringBuilder.append("&album=" + URLEncoder.encode(map.get(TAG_ALBUM), "UTF-8")); albumURL = new RetrieveAlbumArtUrlTask().execute(stringBuilder.toString()).get(); } catch (UnsupportedEncodingException e) { albumURL = null; } catch (InterruptedException e) { albumURL = null; } catch (ExecutionException e) { albumURL = null; } catch (IllegalArgumentException e) { albumURL = null; } map.put("albumArtUrl", albumURL); playcutIndex = playcutIndex + 1; } if(minID == talksetArr[talksetIndex].chronID) { map.put(TAG_SONG, "Talkset"); map.put(TAG_ARTIST, null); map.put(TAG_LAYOUT, "Talkset"); map.put(TAG_CHRONID,""+talksetArr[talksetIndex].chronID); talksetIndex = talksetIndex + 1; } if(minID == breakpointArr[breakpointIndex].chronID) { map.put(TAG_SONG, "Breakpoint"); map.put(TAG_ARTIST, null); map.put(TAG_LAYOUT, "Breakpoint"); map.put(TAG_HOUR, ""+breakpointArr[breakpointIndex].hour); map.put(TAG_CHRONID,""+breakpointArr[breakpointIndex].chronID); breakpointIndex = breakpointIndex + 1; } map.put("Clicked", "False"); oslist.add(map); chronIDCheck = oslist.get(0).get(TAG_CHRONID); i++; } /* If this is the first JSON Parse, we instantiate the adapter, otherwise we just update */ if(firstCall) { list = (ListView) rootView.findViewById(R.id.list); adapter = new LiveAdapter(LiveStreamFragment.this.getActivity(), oslist, LiveStreamFragment.this, list); list.setAdapter(adapter); firstCall = false; } else { if(!adapter.chronIdCheck().equals(oslist.get(0).get(TAG_CHRONID))) { //adapter.updateDataList(oslist); update_button.setVisibility(View.VISIBLE); buttonActivated = true; } } } } }
Here is my new doInBackground() Code:
@Override protected Void doInBackground(String... args) { jParser = new JSONParser(); json = jParser.getJSONFromUrl(wxycUrl); Log.v("TEST","BACKGROUND"); try { playcuts = json.getJSONArray(TAG_PLAYCUTS); talksets = json.getJSONArray(TAG_TALKSETS); breakpoints = json.getJSONArray(TAG_BREAKPOINTS); playcutArr = new Playcut[playcuts.length()]; talksetArr = new Talkset[talksets.length()]; breakpointArr = new Breakpoint[breakpoints.length()]; for(int i = 0; i < playcuts.length(); i++){ JSONObject playcut = playcuts.getJSONObject(i); playcutArr[i] = new Playcut( playcut.getString(TAG_SONG), playcut.getString(TAG_ARTIST), playcut.getString(TAG_ALBUM), playcut.getInt(TAG_CHRONID)); } for(int j = 0; j < talksets.length(); j++){ JSONObject talkset = talksets.getJSONObject(j); talksetArr[j] = new Talkset( talkset.getInt(TAG_CHRONID)); } for(int k = 0; k < breakpoints.length(); k++){ JSONObject breakpoint = breakpoints.getJSONObject(k); breakpointArr[k] = new Breakpoint( breakpoint.getInt(TAG_CHRONID), breakpoint.getLong(TAG_HOUR) ); } } catch (JSONException e) { e.printStackTrace(); } int playcutIndex = 0; int talksetIndex = 0; int breakpointIndex = 0; int minID; int i = 0; /******** Algorithm to consolidate playcuts, breakpoints, and talksets into one arraylist by their chronological ID ********/ while(i < 30){ HashMap<String, String> map = new HashMap<String, String>(); minID = Math.max( playcutArr[playcutIndex].chronID, (int) Math.max( talksetArr[talksetIndex].chronID, breakpointArr[breakpointIndex].chronID ) ); if(minID == playcutArr[playcutIndex].chronID) { map.put(TAG_SONG, playcutArr[playcutIndex].song); map.put(TAG_ARTIST, playcutArr[playcutIndex].artist); map.put(TAG_ALBUM, playcutArr[playcutIndex].album); map.put(TAG_LAYOUT, "Playcut"); map.put(TAG_CHRONID,""+playcutArr[playcutIndex].chronID); StringBuilder stringBuilder = new StringBuilder("http://ws.audioscrobbler.com/2.0/"); stringBuilder.append("?method=album.getinfo"); stringBuilder.append("&api_key="); stringBuilder.append("2ead17554acf667f27cf7dfd4c368f15"); String albumURL = null; try { stringBuilder.append("&artist=" + URLEncoder.encode(map.get(TAG_ARTIST), "UTF-8")); stringBuilder.append("&album=" + URLEncoder.encode(map.get(TAG_ALBUM), "UTF-8")); albumURL = new RetrieveAlbumArtUrlTask().execute(stringBuilder.toString()).get(); } catch (UnsupportedEncodingException e) { albumURL = null; } catch (InterruptedException e) { albumURL = null; } catch (ExecutionException e) { albumURL = null; } catch (IllegalArgumentException e) { albumURL = null; } map.put("albumArtUrl", albumURL); playcutIndex = playcutIndex + 1; } if(minID == talksetArr[talksetIndex].chronID) { map.put(TAG_SONG, "Talkset"); map.put(TAG_ARTIST, null); map.put(TAG_LAYOUT, "Talkset"); map.put(TAG_CHRONID,""+talksetArr[talksetIndex].chronID); talksetIndex = talksetIndex + 1; } if(minID == breakpointArr[breakpointIndex].chronID) { map.put(TAG_SONG, "Breakpoint"); map.put(TAG_ARTIST, null); map.put(TAG_LAYOUT, "Breakpoint"); map.put(TAG_HOUR, ""+breakpointArr[breakpointIndex].hour); map.put(TAG_CHRONID,""+breakpointArr[breakpointIndex].chronID); breakpointIndex = breakpointIndex + 1; } map.put("Clicked", "False"); oslist.add(map); chronIDCheck = oslist.get(0).get(TAG_CHRONID); i++; } Log.v("Test", "Background 2"); return null; } @Override protected void onPostExecute(Void args) { Log.v("TEST","POST"); /* If this is the first JSON Parse, we instantiate the adapter, otherwise we just update */ if(firstCall) { list = (ListView) rootView.findViewById(R.id.list); adapter = new LiveAdapter(LiveStreamFragment.this.getActivity(), oslist, LiveStreamFragment.this, list); list.setAdapter(adapter); firstCall = false; } else { if(!adapter.chronIdCheck().equals(oslist.get(0).get(TAG_CHRONID))) { //adapter.updateDataList(oslist); update_button.setVisibility(View.VISIBLE); buttonActivated = true; } } } }
解决方案The method
onPostExecute
is called on the UI thread, and you are doing a lot of things in it. Try keeping the code as fromif(firstCall)
inonPostExecute
, because that is where you need to access the UI. The rest of the code above can be moved todoInBackground
, which is invoked on a background thread.From the docs :
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing.
onPostExecute(Result), invoked on the UI thread after the background computation finishes.
这篇关于应用程序在做它的主线程的工作太多了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!