gpt4 book ai didi

java - android.os.NetworkOnMainThreadException 。需要使用异步任务来解决这个问题吗?

转载 作者:太空宇宙 更新时间:2023-11-04 07:21:49 24 4
gpt4 key购买 nike

当我试图获取 ListView 的一些日期时,我收到此错误,当我搜索它时,我发现它的解决方案是使用异步任务,但不知道如何使用它,那么我如何在我的代码中使用它?

这是代码:

package com.example.ms;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.example.ms.XMLParser;
import com.example.ms.LazyAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class PhotosActivity extends Activity {

ListView list;
LazyAdapter adapter;
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String URL = "http://api.androidhive.info/music/music.xml";

@Override
protected void onCreate(Bundle savedInstanceState) {

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);

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);
}

list = (ListView) findViewById(R.id.photoslist);

// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);


// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {


}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.photos, menu);
return true;
}

}

最佳答案

是的,您必须运行解析代码,尤其是进行 http 调用 parser.getXmlFromUrl(URL); 的调用;在一个单独的线程中。即在 AsyncTask 中。因此,将代码复制到 AsyncTask.doInBackground() 上,并从 doInbackground() 返回已解析的列表。在 AsyncTask.onPostExecution(List) 中,您将收到解析后的列表,这就是设置 ListView 适配器的正确位置,因为此方法将在主 UI 线程上执行。

公共(public)类 PhotosActivity 扩展了 Activity {

private XmlParserTask task;

public void onCreate(Bundle saved){
super.onCreate(Bundle saved);
// Do your UI setup
task = new XmlParserTask();
task.execute(URL);
}


public void onDestroy(){
super.onDestroy();
// Avoid Memory leaks and android view not attached to window manager Exceptions
if (task != null)
task.cancel(true);
}


private class XmlParserTask extends AsyncTask<String, Void, List<YouSongType>> {

public List<YourSongType> doInBackground(String ... params){

String url = params[0];

List<YourSongType> resultList = new ArrayList<YourSongType>();

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);

// Add your xml parsed data in the list
resultList.add();

return resultList;
 }


public void onPostExecution(List<YourSongType> songsList){
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
}


}

关于java - android.os.NetworkOnMainThreadException 。需要使用异步任务来解决这个问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19207257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com