gpt4 book ai didi

android - 从 URL 解析 XML 并陷入循环

转载 作者:行者123 更新时间:2023-11-29 01:22:58 24 4
gpt4 key购买 nike

我正在尝试从 URL 解析 XML 但没有获取任何数据,当我调试时我发现我卡​​在了 while() 中。环形。在我放弃之前,变量 i 达到了 160。我不明白为什么我会卡在 while 循环中而没有进入循环中的任何 if 语句。

public class Task extends AsyncTask<Void,Void,Void> {
private List<Task> tasks;

public Task()
{

}

@Override
protected Void doInBackground(Void... params) {
makeTask();
return null;
}

public Task(String title){
mTitle = title;
}

private List<Task> makeTask(){
int i = 0;
tasks = new ArrayList<>();

try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL url = new URL("http://xxx");
InputStream stream = url.openStream();
xpp.setInput(stream, null);
int eventType = xpp.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
}
else if (eventType == XmlPullParser.END_DOCUMENT) {
}
else if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("Task")) {
tasks.add(new Task(xpp.nextText()));
Log.d("Task: ", xpp.nextText());
}
else if (eventType == XmlPullParser.END_TAG) {
}
else if (eventType == XmlPullParser.TEXT) {
}
eventType = xpp.next();
}
i++;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return tasks;
}

编辑:

这是新的 while()循环和工作感谢@imsiso

while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("name")) {
tasks.add(new Task(xpp.nextText()));
}
eventType = xpp.next();
}
else
eventType = xpp.next();

代码中的以下行 List<Task> tasks = task.getTasks();给出错误:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

我认为我收到该错误是因为我没有等待 AsyncTask完成但不知道该怎么做。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

/*Removed irrelevant code here*/

Task task = new Task();
new Task().execute();


List<Task> tasks = task.getTasks();

mAdapter = new TaskAdapter(tasks);
mTaskRV.setAdapter(mAdapter);
return view;
}

最佳答案

这是一个 TaskLoader 和 Loader 的回调示例。

private LoaderManager.LoaderCallbacks<List<TaskModel>> mLoaderCallbacks = new LoaderManager.LoaderCallbacks<List<TaskModel>>()
{
@Override
public Loader<List<TaskModel>> onCreateLoader(int id, Bundle args)
{
return new TaskLoader(getContext());
}

@Override
public void onLoadFinished(Loader<List<TaskModel>> loader, List<TaskModel> data)
{
mTaskRV.setAdapter(new TaskAdapter(data));
}

@Override
public void onLoaderReset(Loader<List<TaskModel>> loader)
{
//
}
};

还有任务加载器。

private static class TaskLoader extends AsyncTaskLoader<List<TaskModel>>
{
private static final String TAG = TaskLoader.class.getSimpleName();

private List<TaskModel> mData = null;


public TaskLoader(Context context)
{
super(context);
}

@Override
protected void onStartLoading()
{
super.onStartLoading();
if(mData != null){
deliverResult(mData);
}
if(takeContentChanged() || mData == null){
forceLoad();
}
}

@Override
public void deliverResult(List<TaskModel> data)
{
mData = data;
super.deliverResult(data);
}

@Override
public List<TaskModel> loadInBackground()
{
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL url = new URL("http://xxx");
InputStream in = null;
try{
in = url.openStream();
xpp.setInput(in, "UTF-8");
return parseTasks(xpp);
}
finally{
if(in != null){
in.close();
}
}
}
catch(MalformedURLException e){
Log.e(TAG, "loadInBackground", e);
}
catch(XmlPullParserException e){
Log.e(TAG, "loadInBackground", e);
}
catch(IOException e){
Log.e(TAG, "loadInBackground", e);
}
return null;
}

private List<TaskModel> parseTasks(XmlPullParser xpp)
throws XmlPullParserException, IOException
{
ArrayList<TaskModel> tasks = new ArrayList<>();
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
if(eventType == XmlPullParser.START_TAG){
if(xpp.getName().equalsIgnoreCase("description")){
tasks.add(new TaskModel(xpp.nextText()));
}
}
eventType = xpp.next();
}
return tasks;
}
}

关于android - 从 URL 解析 XML 并陷入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35673990/

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