chartList = new getListTask().execute(getListEndPoint, "").get();
This is my AsyncTask:
public class getListTask extends AsyncTask<String, Integer, ArrayList<ChartInfo>> {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(GetList.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected ArrayList<ChartInfo> doInBackground(String... s) {
URL url;
ArrayList<ChartInfo> list = null;
try {
url = new URL(s[0]);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + s[0]);
}
byte[] bytes = s[1].getBytes();
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.connect();
OutputStream out = conn.getOutputStream();
out.write(bytes);
InputStream stream = conn.getInputStream();
list = readStream(stream);
stream.close();
out.close();
} catch (IOException e) {
// Failed to connect to server, return null
return null;
} catch (XmlPullParserException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return list;
}
@Override
protected void onPostExecute(ArrayList<ChartInfo> result) {
progressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer...values) {
progressDialog.setProgress(values[0]);
}
/**
* Parses the xml received from input stream
*/
private ArrayList<ChartInfo> readStream(InputStream is) throws IOException, XmlPullParserException {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(is, null);
ArrayList<ChartInfo> list = new ArrayList<ChartInfo>();
ChartInfo chart = new ChartInfo();
int eventType = parser.getEventType();
int size = 0;
int position = 1;
int counter = 0, year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
// Get the total number or charts in list; should be first text
while (eventType != XmlPullParser.END_DOCUMENT) {
parser.next();
eventType = parser.getEventType();
if (eventType == XmlPullParser.TEXT) {
size = Integer.parseInt(parser.getText());
break;
}
}
// Parse rest of xml
do {
publishProgress(position / size * 100);
parser.next();
eventType = parser.getEventType();
if (eventType == XmlPullParser.TEXT) {
if (counter == 0) {
chart = new ChartInfo();
chart.setName(parser.getText());
counter++;
} else if (counter == 1) {
chart.setType(parser.getText());
counter++;
} else if (counter == 2) {
year = Integer.parseInt(parser.getText());
counter++;
} else if (counter == 3) {
month = Integer.parseInt(parser.getText()) - 1; // Android Calendar is 0-11, server is 1-12, so subtract 1
counter++;
} else if (counter == 4) {
day = Integer.parseInt(parser.getText());
counter++;
} else if (counter == 5) {
hour = Integer.parseInt(parser.getText());
counter++;
} else if (counter == 6) {
minute = Integer.parseInt(parser.getText());
counter++;
} else if (counter == 7) {
second = Integer.parseInt(parser.getText());
chart.setServerDate(year, month, day, hour, minute, second);
counter = 0;
list.add(chart);
position++;
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);
return list;
}
}
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment