public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.data);
String serviceUrl = "-"; // I can not show it
String serviceKey = "-"; // I can not show it
serviceKey = URLEncoder.encode(serviceKey);
String strSrch = "406";
String strUrl = serviceUrl + "?ServiceKey=" + serviceKey + "&strSrch=" + strSrch;
new DownloadWebpageTask().execute(strUrl);
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackgroud(String... urls) {
try {
return (String)downloadUrl((String)urls[0]);
} catch (IOException e) {
return "Download Fail";
}
}
protected void onPostExecute(String result) {
tv.append(result + "\n");
tv.append("=======parsing result=========\n");
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(result));
int eventType = xpp.getEventType();
boolean bSet = false;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
;
} else if (eventType == XmlPullParser.START_TAG) {
String tag_name = xpp.getName();
if (tag_name.equals("busRouteNm") || tag_name.equals("busRouteId"))
bSet = true;
} else if (eventType == XmlPullParser.TEXT) {
if (bSet) {
String content = xpp.getText();
tv.append(content + "\n");
bSet = false;
}
} else if (eventType == XmlPullParser.END_TAG) {
;
}
eventType = xpp.next();
}
} catch (Exception e) {
tv.setText(e.getMessage());
}
}
private String downloadUrl(String myurl) throws IOException {
HttpURLConnection conn = null;
try {
URL url = new URL(myurl);
conn = (HttpURLConnection) url.openConnection();
BufferedInputStream buf = new BufferedInputStream(conn.getInputStream());
BufferedReader bufreader = new BufferedReader(new InputStreamReader(buf, "utf-8"));
String line = null;
String page = "";
while ((line = bufreader.readLine()) != null) {
page += line;
}
return page;
} finally {
conn.disconnect();
}
}
}
}
「私有類DownloadWebpageTask擴展的AsyncTask」 此行有錯誤:MainActivity.DownloadWebpageTask不是抽象和的AsyncTask不重寫抽象方法doInBackground(字符串...)
Error:(44, 13) error: MainActivity.DownloadWebpageTask is not abstract and does not override abstract method doInBackground(String...) in AsyncTask
你忘了'Background' –