2012-08-29 171 views
0

我試圖從URL頁面解析XML。爲此,我使用了this IBM example中解釋的SAX實施,Adapter和我從this article得到的其他更改。我也試着實現AsyncTask來解析並顯示一個ProgressDialog,但我認爲這是我的應用程序開始崩潰的地方。在Android上使用Sax解析Xml

我真的不知道如何在代碼中實現AsyncTask,我相信我糟糕的實現會導致我的應用程序強制關閉。

MainActivity:

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
ListView lv1; 
ProgressDialog ShowProgress; 
public static ArrayList<MangaItem> MangaItemList = new ArrayList<MangaItem>(); 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    lv1 = (ListView) findViewById(R.id.listView1); 

    ShowProgress = ProgressDialog.show(MainActivity.this, "", 
      "Loading. Please wait...", true); 
    //new loadingTask().execute("http://www.mangapanda.com/alphabetical"); 
    new loadFeedTask().execute(); 

    lv1.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri 
        .parse(MangaItemList.get(position).getMangaLink())); 
      startActivity(intent); 

     } 
    }); 

} 

class loadFeedTask extends AsyncTask<String, Void, ArrayList<MangaItem>> { 
    private String feedUrl; 

    protected void onPostExecute(String s) { 
     lv1.setAdapter(new EfficientAdapter(MainActivity.this, MangaItemList)); 
     //new MangaParserTask().execute(); 
     ShowProgress.dismiss(); 

    } 

    protected ArrayList<MangaItem> doInBackground(String... params) { 
     ArrayList<MangaItem> ParsedMangaItemList = new ArrayList<MangaItem>(); 
     feedUrl = "http://www.mangapanda.com/alphabetical"; 
     FeedParser parser = new SaxFeedParser(feedUrl); 
     ParsedMangaItemList = parser.parse(); 
     for (MangaItem mitem : ParsedMangaItemList) { 
      MangaItemList.add(mitem); 
     } 
     return MangaItemList; 
    } 


} 
} 

我如何正確使用AsyncTask使我的解析器將返回一個ArrayList,我可以再放入ArrayAdapter

+0

你的方法是正確的。怎麼了? – adatapost

+0

@AVD當我運行它時,我的應用程序將強制關閉。 Logcat不會給出任何錯誤,但在DalvikVM下面有兩個'AsyncTask''線程'和一個在其旁邊的「暫停運行時異常」。這就像我調試這個 – Davidrd91

+0

一樣多,我找到了一些*無關的代碼。看看答案。 – adatapost

回答

1

在子類(AsyncTask<Params, Progress, Result>)中不恰當地使用類型參數。重新編寫AsyncTask子類。

class loadFeedTask extends AsyncTask<String, Void, ArrayList<MangaItem>> { 
    protected void onPostExecute(ArrayList<MangaItem> list) { 
     lv1.setAdapter(new EfficientAdapter(MainActivity.this, list)); 
     ShowProgress.dismiss(); 
    } 
    protected ArrayList<MangaItem> doInBackground(String... params) { 
     ArrayList<MangaItem> list=null; 
     String feedUrl = "http://www.mangapanda.com/alphabetical"; 
     FeedParser parser = new SaxFeedParser(feedUrl); 
     list = parser.parse(); 
     MangaItemList=list; 
     return list; 
    } 
    } 
1

使用此代碼

try { 

     items = new ArrayList<String>(); 

     XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     XmlPullParser xpp = factory.newPullParser(); 
     xpp.setInput(new InputStreamReader(
       getUrlData(" url"))); 

     while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) { 
      Log.i(TAG, "doc started"); 
      if (xpp.getEventType() == XmlPullParser.START_TAG) { 
       if (xpp.getName().equals("entry")) { 
        items.add(xpp.getAttributeValue(0)); 
       } 
      } 
      xpp.next(); 

     } 
    } catch (Throwable t) { 
     Toast.makeText(this, "Request failed: " + t.toString(), 
       Toast.LENGTH_LONG).show(); 
    } 

GET網址數據方法

public InputStream getUrlData(String url) throws URISyntaxException, 
     ClientProtocolException, IOException { 

    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpGet method = new HttpGet(new URI(url)); 
    HttpResponse res = client.execute(method); 
    return res.getEntity().getContent(); 
} 
+0

本例使用'XmlPullParser'而不是'SaxParser'。我將不得不重寫這個處理程序,但我會嘗試讓它。讓你知道它是怎麼回事。 – Davidrd91

+0

對不起noob問題,但它說「方法getUrlData()未定義類型Mainactivity.loadfeedtask」 – Davidrd91