Use progressDialog while parsing xml - android

this is my first post on stackoverflow and i'm new to Android. I already searched the forum for a similar question and found this one: SAX parser progress monitoring.
But unfortunately it doesn't help me.
My app has to register events in the calendar.
The first thing i achieved was to progress the download of a xml file. You can see this in the attached code.
After that i want the progressDialog to reset and start tracking the progress of the parsing. Would be great if this could be done with a new setMessage() and a percentage value.
After the parsing the data is used to register events in the calendar, again with the same progressDialog.
But at first i want to know how i can track the progress of the parsing. Would be great if have some ideas.
Thank you
public class AddCoursesToCalendar extends Activity {
public static final int progress_bar_type = 0;
ArrayList<String> selectedCourses;
public ProgressDialog progressDialog;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_add_courses_to_calendar);
if (bundle != null) {
selectedCourses = bundle.getStringArrayList("selectedCourses");
}
new GetDataTask().execute();
}
private Boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected())
return true;
return false;
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Downloading file. Please wait...");
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.show();
return progressDialog;
default:
return null;
}
}
public class GetDataTask extends AsyncTask<String, String, Integer> {
private static final String URL = "http://10.0.2.2/WIN2.xml";
private static final String KEY_ITEM = "Item";
private static final String KEY_DAUER = "Duration";
private static final String KEY_ENDE = "End";
private static final String KEY_SEMESTER_DOZENT = "Location";
private static final String KEY_RAUMMITSTOCKWERK = "Organizer";
private static final String KEY_START = "Start";
private static final String KEY_VERANSTALTUNGSNAME = "Subject";
#Override
protected void onPreExecute() {
super.onPreExecute();
onCreateDialog(progress_bar_type);
}
#Override
protected Integer doInBackground(String... params) {
if (isOnline()) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL, this);
long id = 0;
Document doc = parser.getDomElement(xml, this);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++){
Element e = (Element) nl.item(i);
for (String s : selectedCourses) {
if (parser.getValue(e, KEY_VERANSTALTUNGSNAME)
.contains(s)) {
String dozent = null;
int spaceIndex = parser.getValue(e,
KEY_SEMESTER_DOZENT).indexOf(" ");
int lastIndex = parser.getValue(e,
KEY_SEMESTER_DOZENT).length();
if (spaceIndex != -1) {
dozent = parser
.getValue(e, KEY_SEMESTER_DOZENT)
.substring(spaceIndex, lastIndex);
}
addEvent(
parser.getValue(e, KEY_VERANSTALTUNGSNAME),
parser.getValue(e, KEY_START),
parser.getValue(e, KEY_ENDE),
parser.getValue(e, KEY_DAUER),
dozent,
parser.getValue(e, KEY_RAUMMITSTOCKWERK),
id);
} id++;
}
}
} else {
Toast.makeText(AddCoursesToCalendar.this, "No Connection..",
Toast.LENGTH_LONG).show();
}
return 1;
}
public void doProgress(String value){
publishProgress(value);
}
#Override
protected void onPostExecute(Integer result) {
progressDialog.dismiss();
super.onPostExecute(result);
}
protected void onProgressUpdate(String... progress) {
progressDialog.setProgress(Integer.parseInt(progress[0]));
}
protected void addEvent(String title, String start, String end,
String duration, String organizer, String location, long id) {
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
long startInMillis = 0, endInMillis = 0;
Date startDate, endDate;
try {
startDate = format.parse(start);
endDate = format.parse(end);
startInMillis = startDate.getTime();
endInMillis = endDate.getTime();
} catch (ParseException e1) {
e1.printStackTrace();
}
TimeZone timeZone = TimeZone.getDefault();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.DTSTART, startInMillis);
values.put(CalendarContract.Events.DTEND, endInMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.EVENT_LOCATION, "Location: " + location);
values.put(CalendarContract.Events.CALENDAR_ID, 1);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
ContentUris.withAppendedId(uri, id);
}
}
}
And here the class responsible for the downloading and parsing:
public class XMLParser {
public String getXmlFromUrl(String url, AddCoursesToCalendar.GetDataTask task) {
String xml = null;
try {
int count;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
long lenghtOfFile = httpEntity.getContentLength();
byte data[] = new byte[1024];
long total = 0;
while ((count = httpEntity.getContent().read(data)) != -1) {
total += count;
task.doProgress(""+(int)((total*100)/lenghtOfFile));
}
xml = EntityUtils.toString(httpEntity, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
public Document getDomElement(String xml, AddCoursesToCalendar.GetDataTask task) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
StringReader reader = new StringReader(xml);
is.setCharacterStream(reader);
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
}
I hope the style of my first post was ok, if not please tell me. Thank you in advance

I assume that the XML is very large and that the parsing takes at least 30 seconds. Otherwise, do yourself a favor and just show an indeterminate indicator ;)
Also, to simplify your code, you may want to use URL.openStream() instead of a full featured HttpClient, and switch to a simple GET (since you don't send any parameter I wonder why your server requires a POST).
Assumed that the file is a very big one (for a phone, of course), memory usage must be taken into account, and you'll switch from the DOM to the SAX interface to process the XML. SAX provides you with an event-based interface while it scans the stream, so you don't have to load the entire file in memory before even starting to process it. Remember, it's a big file and we don't want to run out of memory.
Using SAX we can parse the file while it's downloading, so we can leave network latencies off when computing total and remaining time. At this point, the progress can be approximated by current item / total. You can update an internal counter to keep track of the current item, but the problem now is how to compute the total. You can think in terms of
Bytes (eg processed 12932/2791290 Bytes)
Business items (eg processed 80/291 Items)
In both cases you need some support from the server. It should provide a Content-Length HTTP header or a preamble (called <manifest> in this snippet):
<root>
<manifest>
<total>291</total>
</manifest>
...
<item id="foobar1">
<foo>Foo</foo>
<bar>Bar</bar>
</item>
...
</root>
The AsyncTask part is quite easy: you can use publishProgress() inside your doInBackground(), and in turn it will invoke onProgressUpdate() on the UI thread. Inside this method, you will update the progress of the dialog.
Just a couple of final notes: when the screen is rotated (default configuration assumed), your activity is destroyed, and then recreated. Note that dialogs shown via Activity.showDialog are automatically recreated by the system, but the old AsyncTask keeps running, and it may keep references to the old (now probably destroyed but not garbage collected) Activity and the old dialog. You have to work on this on your own, there are too many alternatives (including the Loader framework).
I hope you understand now that the task is not as simple as it might sound, so my advice is to carefully examine business requirements and come up with the simplest and more robust solution - after all, downloading an XML and showing a progress dialog is not the main task of your app ;)

Related

Android XML Multi-Node Reading Using Current Date

I'm creating a weather app which gets the min/max temperature from a 5 day forecast XML with same node names. I want to use the current date to look through the XML and find the correct min/max for that day.
This is the weather XML: Link
Here is my code, I've trimmed it just enough to the part where I don't understand the multi-nodes, but still I wanted it to be reusable (Currently it just gets the first min/max as denoted by a 0):
public class MyAsyncTask extends AsyncTask < Void, Void, String > {
//========================== pre execute to get date for xml =======
protected void onPreExecute() {
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
} catch (Exception e) {}
}#
Override
protected String doInBackground(Void...params) {
//=========================== Load data using xml ================
try {
URL xmlUrl2 = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=london&mode=xml&units=metric&cnt=5");
InputStream inm = xmlUrl2.openStream();
Document docm = parsem(inm);
docm.getDocumentElement().normalize();
Node nNodem = docm.getElementsByTagName("temperature").item(0);
Element eElementm = (Element) nNodem;
double dmax = Math.round(Double.parseDouble(eElementm.getAttribute("max")));
int dxmax = (int) dmax;
xmaxtemp = Integer.toString(dxmax);
double dmin = Math.round(Double.parseDouble(eElementm.getAttribute("min")));
int dxmin = (int) dmin;
xmintemp = Integer.toString(dxmin);
} catch (UnknownHostException s) {
internet = false;
} catch (IOException i) {
System.out.println("IO Exception error!");
} catch (Exception ex) {
ex.printStackTrace();
}
return xtemp;
}
//========================= show data===============
#
Override
protected void onPostExecute(String result) {
TextView minmax = (TextView) findViewById(R.id.minmax);
minmax.setText("↑" + xmaxtemp + " " + xmintemp + "↓");
}
//======================== parse document =======
public static Document parse(InputStream is) {
Document ret = null;
DocumentBuilderFactory domFactory;
DocumentBuilder builder;
try {
domFactory = DocumentBuilderFactory.newInstance();
domFactory.setValidating(false);
domFactory.setNamespaceAware(false);
builder = domFactory.newDocumentBuilder();
ret = builder.parse(is);
} catch (Exception ex) {
System.err.println("unable to load XML: " + ex);
}
return ret;
}
}
For better use, u should use xpath to have a perfect manipulation over ur xml data:
this is an example how to get all temperature nodes
String expression = "//temperature";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
After that you can manipulate the list.
this is a good tuto to start using xpath with java:
java-xml-xpath-tutorial/

Unable to start activity ComponentInfo{

I have the following mistake:
12-05 20:35:31.005: E/AndroidRuntime(1084): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helplawyer/com.example.helplawyer.AndroidXMLParsingActivity}: android.os.NetworkOnMainThreadException
in xml manifest I added <uses-permission android:name="android.permission.INTERNET" />
this is my main class
package com.example.helplawyer;
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://www.consultant.ru/rss/hotdocs.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_DATE = "pubDate";
static final String KEY_LINK = "link";
static final String KEY_DESC = "description";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainxml);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_TITLE, KEY_DESC, KEY_DATE, KEY_LINK }, new int[] {
R.id.name, R.id.description, R.id.cost, R.id.link });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
String link = ((TextView) view.findViewById(R.id.link)).getText().toString();
String description = ((TextView) view.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, name);
in.putExtra(KEY_DATE, cost);
in.putExtra(KEY_LINK, link);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
}
XMLParser
package com.example.helplawyer;
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
I read on stackoverflow that I need to use new Thread(new Runnable() {
but I don't understand how to rewrite my code that my app works good.
Please, help me with my problem!
Thank you
From the Android documentation.
The exception that is thrown when an application attempts to perform a
networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or
higher. Applications targeting earlier SDK versions are allowed to do
networking on their main event loop threads, but it's heavily
discouraged. See the document Designing for Responsiveness.
Also see StrictMode.
Look here for examples of running this operation in the background (Find "How to Avoid ANRs")
You cannot access network from your main thread.
you need to use AsyncTask . Check my answer at
Android Connecting with PHP and MySql Force Close
Note the AsyncTask<String, Void, String>, here first String is the parameter to doInBackground , Void is parameter to onProgressUpdate(), we are not implementing this method, second String is parameter to onPostExecute()
Now, from MainActivity when you call new InnerClass().execute(urltoxml); Android will automatically call doInBackground() and will pass urltoxml as the argument`
Do all your network stuff inside doInbackground, when finished, doInBackground will return a result string and android will pass that as the parameter to onPostExecute().
Use that string however you want from onPostExecute
public class MainActivity extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View v){
new InnerClass().execute(urltoxml);
}
});
}
public class InnerClass extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... arg0) {
String returnVal;
//process your xml here
//store the result in returnVal;
return returnVal;
}
#Override
protected void onPostExecute(String result) {
//do something with the data
}
}
}
You are running Network Request on main UI thread.
Android >=3.0 does not allow to run Network Request on main UI thread. You need to use
AsyncTask to do network operation.

The application has stopped unexpectedly, specific case on android

Ok, I am working on one android application. It has many features, such as showing the News, Horoscope, TV schedule, Weather forecast ... Every piece of information is comming to me through RSS, using XML. Application works as it is supposed to do when I have wifi or 3G, but as soon as there is no wifi or 3G signal, or some of the links are down under maintenance I get kicked out of application and error like:
The application has stopped unexpectedly! Please Try Again.
I was trying to make some Activity that will show something like:
There was a problem with your request! Please make sure that wifi or 3G signals are available or try again later.
I've tried many ways but nothing seams to work. Here are a few classes that might help:
1.
public class Pocetna extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pocetna);
.
.
.
vijesti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent xw = new Intent(getApplicationContext(), Vijesti.class );
xw.putExtra("A", "http://klix.ba/rss/naslovnica");
startActivity(xw);
}
});
2.
public class Vijesti extends ListActivity {
static String url =null;
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_DATE = "pubDate";
static final String KEY_DESC = "encoded";
static final String UVOD = "uvod";
static final String CLANAK = "clanak";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vijesti_m);
Intent in = getIntent();
// Get XML values from previous intent
url = in.getStringExtra("A");
final ArrayList<HashMap<String,String>> menuItems = new ArrayList<HashMap<String,String>>();
ArrayList<String> xqw = new ArrayList<String>();
ParserVijesti parser=null;
Document doc=null;
try {
parser = new ParserVijesti();
String xml = parser.getXmlFromUrl(url); //get XML
doc = parser.getDomElement(xml);
} catch (Exception e1) {
finish();
}
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
//loop
for (int i=0; i< nl.getLength(); i++){
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, String> mapq = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//add to map
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(UVOD, parser.getValue(e,UVOD));
map.put(CLANAK, parser.getValue(e,CLANAK));
menuItems.add(map);
xqw.add(parser.getValue(e,KEY_TITLE));
}
for(int gf=0; gf<xqw.size(); gf++){
Log.w("ISPISI: ", xqw.get(gf));
}
ArrayAdapter adapterx = new ArrayAdapter(this, R.layout.vijesti_m,R.id.tetkica, xqw);
setListAdapter(adapterx);
//singleView
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
int hg = position;
HashMap<String, String> kaktus = menuItems.get(hg);
String uvod1 = kaktus.get(UVOD);
String clanak1 = kaktus.get(CLANAK);
String dat1 = kaktus.get(KEY_DATE);
String tit1 = kaktus.get(KEY_TITLE);
//intent
Intent inx = new Intent(getApplicationContext(), VijestiSingle.class);
inx.putExtra(KEY_TITLE, tit1);
inx.putExtra(KEY_DATE, dat1);
inx.putExtra(UVOD, uvod1);
inx.putExtra(CLANAK, clanak1);
startActivity(inx);
}
});
}
}
3.
public class ParserVijesti {
// constructor
public ParserVijesti() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
dbf.setNamespaceAware(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setByteStream(new ByteArrayInputStream(xml.getBytes("UTF-8")));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if(child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE){
return child.getNodeValue();
}
}
}
}
return "";
}
public final String getElementValue2( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if(child.getNodeType() == Node.CDATA_SECTION_NODE){
return child.getNodeValue();
}
}
}
}
return "SRANJE";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public String getValue3(Element item, String str){
NodeList n = item.getElementsByTagNameNS("http://purl.org/rss/1.0/modules/content/", str);
String ses = this.getElementValue2(n.item(0));
//String mim =ses.replaceAll("(?s)\\<.*?\\>", " \n");
String html = ses;
Spanned strxa = Html.fromHtml(html);
String fffx=strxa.toString();
//return this.getElementValue2(n.item(0));
//return ses;
//return Promjena(ses);
return fffx;
}
}
So basically, what I want to do is not to check whether there is or not wifi or 3G. I just want to implement that Activity that will show that there is an error and not allow instantly kicking out of application whenever error occures. Please, anyone?
To check if the device is connected to Wifi or 3G, you could create a method like this:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if(netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
Then, if the Activity you are trying to launch is dependent on internet connection, you could do:
if(!isOnline) {
Toast.makeText(getApplicationContext(), "You are not connected to the internet", Toast.LENGTH_SHORT).show();
} else {
startActivity(*yourIntent);
}
You'll need these permissions in your manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Call web service in android 4.1

I have this code here that worked all this time in 2.3 now we have to update it and I am getting a lot of errors like NetworkOnMainThreadException. I want to go and grab a xml from my web service, bring it down and parse it into an array list. Here is the code
//Gets the xml from the url specified
String CallWebService(String url){
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
//Parses the xml to get DOM element
public Document GetDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
//Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
//Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
//Log.e("Error: ", e.getMessage());
return null;
}
// return DOM
return doc;
}
//Gets the child nodes of the xml
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if(child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE){
return child.getNodeValue();
}
}
}
}
return "";
}
I also have a getChildElements method. The problem is when I call this method. I used to do so like this:
String serviceURL = "http://webservice.example.com/";
String xml = CallWebService(serviceURL);
Document doc = GetDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName("details");
getChildElements(nl);
But now in 4.1 I need to do this asynchronously and I don't know how. Any help will be greatly appreciated.
EDIT
Here is what i have bu the Thread does not start
final String serviceURL = "urlString";
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
if(msg.what == JOB_COMPLETE) {
String xml = (String) msg.obj;
Document doc = GetDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName("details");
getChildElements(nl);
}
super.handleMessage(msg);
}
};
Thread t = new Thread() {
#Override
public void run() {
String xml = CallWebService(serviceURL);
Message msg = Message.obtain(mHandler, JOB_COMPLETE, xml);
msg.sendToTarget();
}
};
t.start();
EDIT
So I am trying the async way and it still wont work. Its not hitting the GetDomElement at all. Here is the code.
//I call this in my onCreate()
new getAppInfo().execute("http://webservice.example.com");
private class getAppInfo extends AsyncTask<String, Void, String> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected String doInBackground(String... urls) {
return CallWebService(urls[0]);
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(String xml) {
Document doc = GetDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName("details");
getChildElements(nl);
}
}
You have to implement an AsyncTask:
http://developer.android.com/reference/android/os/AsyncTask.html
Btw this is needed from Android 3 (if I remember well).
I've implemented this in my app, you can browse my code here: https://github.com/enrichman/roma-tre/blob/master/src/com/roma3/infovideo/utility/rss/RssTask.java
Hope this help
http://www.techrepublic.com/blog/app-builder/using-androids-asynctask-to-handle-long-running-io/670?pg=2&tag=content;siu-container
Define a Handler in your main ui thread in activity onCreate for example
private Handler mHandler;
private static int JOB_COMPLETE = 1;
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
if(msg.what == JOB_COMPLETE) {
String xml = (String) msg.obj;
// do whatever you want with that string
}
super.handleMessage(msg);
}
};
Then run all your long jobs in background thread
final String url = "...........";
Thread t = new Thread() {
#Override
public void run() {
String xml = CallWebService(url);
Message msg = Message.obtain(mHandler, JOB_COMPLETE, xml);
msg.sendToTarget();
}
};
t.start();
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged
see android developers page
Do your network related task using Async task or try using safe threads

NetworkOnMainThread

I get a NetworkOnMainThreadException when I try to implement the following code:
public class HandlingXMLStuff extends ListActivity{
static final String URL = "xml_file";
static final String ITEM = "item"; //parent
static final String Id = "id";
static final String Name = "name";
static final String Desc = "desc";
static final String Link = "Link";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.xmllist);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
xmlparser parser = new xmlparser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(ITEM);
//START: loop through all item nodes <item>
for (int i = 0;i<nl.getLength();i++){
//lets create our HASHMAP!! (feeds items into our ArrayList)
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//add each child node to the HashMap (key, value/<String, String>)
map.put(Name, parser.getValue(e, Name));
map.put(Desc, parser.getValue(e, Desc));
map.put(Link, parser.getValue(e, Link));
menuItems.add(map);
}//DONE
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
new String[] {Name, Desc, Link}, new int []{R.id.name, R.id.description, R.id.link});
setListAdapter(adapter);
}
}
and the handler:
public class xmlparser{
public String getXmlFromUrl(String url) {
String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
}
Any idea why? It should work, all the tutorials I've read treat this as working code but it doesn't run and only throws the exception. I've read I might need to implement asynctask but im new to it and not sure what parts need their own thread. Thanks for any help, critique (constructive), suggestions, etc.
Any idea why?
Because, if that hunk of code is being executed on the main application thread, you are doing network I/O on the main application thread.
I've read I might need to implement asynctask but im new to it and not sure what parts need their own thread.
I would put the network I/O and the parsing in doInBackground() and the setListAdapter() call in onPostExecute() of an AsyncTask.
If you simply want to test your code, and don't want to add any more complications yet, you can add this to your onCreate()
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
You don't want this to be permanent, as network operations on the UI thread makes for a bad experience when using the app, but can be useful when testing.
Adding to CommonsWare answer, the NetworkOnMainThreadException was added sometime between 2.3.3 (Gingerbread_MR1) and 3.0 (Honeycomb). If you look at
android.app.ActivityThread
you will find the following piece of code:
/**
* For apps targetting SDK Honeycomb or later, we don't allow
* network usage on the main event loop / UI thread.
*
* Note to those grepping: this is what ultimately throws
* NetworkOnMainThreadException ...
*/
if (data.appInfo.targetSdkVersion > 9) {
StrictMode.enableDeathOnNetwork();
}
I think the tutorials that you were following were written before this was put into place, and so did not cause the NetworkOnMainThreadException. Follow CommonsWare instructions regarding AsyncTask and you'll fix your error.

Categories

Resources