ClassAsyncTask を別のクラスに使用したいのですが、しようとしていますが、成功していません。これを行う方法についての明確な情報またはアイデアが必要です。前もって感謝します。
public class XMLParser extends Activity {
String targetURL = "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml";
TextView name[], website[], category[];
LinearLayout linearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* layout to display the view */
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(1);
/* Set the ContentView to layout for display */
this.setContentView(linearLayout);
ClassAsyncTask asyncTask = new GBAsyncTask();
asyncTask.execute(targetURL);
}
//
public class ClassAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
/* Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/* Send URL to parse XML Tags */
URL sourceUrl = new URL(targetURL);
/* Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
return null;
}
/* Return-value from doInBackground */
protected void onPostExecute(String result) {
/* Get result from MyXMLHandler SitlesList Object */
SiteList sitesList = MyXMLHandler.sitesList;
/* Assign TextView array length by arrayList size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
int h = sitesList.getName().size();
/* Set the result text in TextView and add it to layout */
for (int i = 0; i < h; i++) {
name[i] = new TextView(getApplicationContext());
name[i].setText("Name = " + sitesList.getName().get(i));
website[i] = new TextView(getApplicationContext());
website[i]
.setText("Website = " + sitesList.getWebsite().get(i));
category[i] = new TextView(getApplicationContext());
category[i].setText("Website Category = "
+ sitesList.getCategory().get(i));
linearLayout.addView(name[i]);
linearLayout.addView(website[i]);
linearLayout.addView(category[i]);
}
}
}
}
ありがとう。