Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Looking for android programmer to help me get started with an app
New Posts  All Forums:Forum Nav:

Looking for android programmer to help me get started with an app

post #1 of 8
Thread Starter 
Hi everyone.
First of all i'm not very good at android programming since I have never done it before. I have done very simple Java and some C#/.NET (And some C, but that doesn't count)
I'm currently trying to create an app that needs to do the following:

The app consists of 5 tabs. And pretty much everything i need to build the app is fetched from a database containing a lot of xml datasets.
I need to parse all of this. I'm trying to do it with an xml pull parser. But I have hit a wall. Everything I try to do just keep crashing the app smile.gif

First of all the tabs need a name. The name for the tabs are in the xml data. I have to parse some xml data before the app is even build. This has presented me with a huge problem.
When the first xml data has been parsed I have the information to create contents inside the tabs (It should be noted i'm using ActionbarSherlock so that I can support more versions of android, this is important). I'm also using fragments for the tabs.

Now. I know how to make the various calls to the web service, so once I can parse the initial data I should be able to move on.

Does anyone here have some experience in xml parsing in android? If so I hope you want to help me smile.gif

If I need to post some code I will do so. But tell me what you want to see (it's currently not working very well). And can you somehow post code in here?

Regards Panzerfury

PS. If you need more info ask (i'm not sure wether i'm allowed to share this web service or not). I can post the xml data sheet if you want me to (I will edit with some other info)
post #2 of 8
I've created something very similar to that. A series of tabs which display data from an xml source. It's not complicated at all. My datasource was online, but my app also made a local copy of the xml, so it would still work if the device was running offline.
The difference is in my app the tabs were hard-coded, not dynamic. But I fail to see your problem. Just read the first xml within your main activity, and then create additional tabs (activities) as needed. There's no need to know the first xml name in advance
Edited by wedge - 7/21/13 at 8:44pm
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #3 of 8
Here's the XML parser class I used. I found originally it in a blog somewhere, I would reference the source, but I can seem to find it. Anyway, it's no longer original, I've made a few special modifications myself.
It should work as-is, but you may also have to add your own tweaks to it too.
XMLParser.java (Click to show)
Code:
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


import android.content.Context;
//import android.util.Log;

public class XMLParser {
        
        Context context;
        
        public XMLParser()
        {
                
        }
        
        public XMLParser(Context _context)
        {
                context = _context;
        }
        

        public String getRSSFromUrl(String url) {  //getRSSFromUrl was created to access the Twitter API, but this function will work for other sources too
        String xml = null;
 
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            //HttpPost httpPost = new HttpPost(url);
            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;
    }
        
        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);
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }
        

        public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
 
                //clean the BOM or any other junk out from the start of the xml
                if (!xml.startsWith("<"))
                {
                        xml = xml.substring(xml.indexOf("<"), xml.length());
                }
                
            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;
    }

        public String getValue(Element item, String str) {
            NodeList n = item.getElementsByTagName(str);
            return this.getElementValue(n.item(0));
        }
        
        public int getIntValue(Element item, String str) {
                NodeList n = item.getElementsByTagName(str);
                int value = 0;
                //if (this.getElementValue(n.item(0)).equals(""))
                //{
                try{
                        value += Integer.parseInt(this.getElementValue(n.item(0)));
                }
                catch (Exception e)
                {
                        
                }
                //}
                //else
                //{
                        return value;
                //}
        }
         
        public int getImage(Element item, String str){
        
                NodeList n = item.getElementsByTagName(str);
                int value = 0;
                //if (this.getElementValue(n.item(0)).equals(""))
                //{
                        value += context.getResources().getIdentifier(this.getElementValue(n.item(0)), null, context.getPackageName());
                //}
                //else 
                //{
                        return value;
                //}
                
        }
        
        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 "";
          } 
    public String convertStreamToString(InputStream is) {
        try {
            return new java.util.Scanner(is).useDelimiter("\\A").next();
        } catch (java.util.NoSuchElementException e) {
            return "";
        }
    }
}

Just add then class to your project.
And then in your code use it like this:
How to use it (Click to show)
Code:

NodeList nl = null;
aSyncTask myAsyncTask = new aSyncTask ();  //All web calls in Android must be asynchronous

try {
        nl = myAsyncTask.execute(URL).get();       //URL is the full url of the xml document to retrieve.  This can be online or it can be local
                                              //NodeList nl represents the complete list of all elements matching the nodeName in the asynctask 

        if (nl != null && nl.getLength() > 0) {
                XMLParser parser = new XMLParser();

               //you can run through all the elements in nl in a loop, like so:
                for (int i = 0; i < nl.getLength(); i++) {       //the Element e represents an element matching the nodeName in the asynctask
                        Element e = (Element) nl.item(i);

                        String id = parser.getValue(e, "id");           //here's where you can get each of the attributes of the current element
                        String name = parser.getValue(e, "name");     //getValue returns a string value
                        int num = parser.getIntValue(e, "numeric");    //getIntValue returns an int
                        int image = parser.getImage(e, "logo");       //getImage returns the unique identifier of an image resource matching the filename specified in the "logo" attribute

                }
        }

} catch (InterruptedException e1) {
        e1.printStackTrace();
} catch (ExecutionException e1) {
        e1.printStackTrace();
}



class aSyncTask extends AsyncTask<String, Void, NodeList>
{
        @Override
        protected NodeList doInBackground(String... params) {
                
                XMLParser parser = new XMLParser();
                String xml = parser.getXmlFromUrl(params[0]);   //you can also use the parser.getRSSFromUrl, depending on the type of data source
                
                if (xml != null) 
                {
                        Document doc = parser.getDomElement(xml);
                
                        NodeList nl = doc.getElementsByTagName("nodeName");  //change the nodeName string to the name of elements you wish to retrieve
                        if (nl.getLength() > 0)
                        {
                                return nl;
                        }
                }
                
                return null;
        }
        
}



Note that as this is written, the thread execution will pause while the data is being retrieved. So this code must not be run in the main thread of the program.
Edited by wedge - 7/21/13 at 8:56pm
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #4 of 8
It might be too late, but if you're looking to parse XML, XPath expressions are really nice. I was used to JSON and really hated parsing XML at first, but XPath expressions made it simple.
The New Guy
(16 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0@3.0GHz GA-P35-DS3L EVGA 8800GT Superclocked 4GB Corsair XMS2 DDR2 800 
Hard DriveHard DriveOptical DriveOS
Seagate 320GB 7200RPM Western Digital Caviar Black Lite-on DVD burner Windows 7 Professional x64 
MonitorKeyboardPowerCase
Samsung 225BW Filco Majestouch Ninja (full keyboard w/browns) Cooler Master Xtreme 650 Antec 900 
MouseAudioAudio
Logitech MX518 Denon AHD-2000 Sennheiser HD595 
  hide details  
Reply
The New Guy
(16 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0@3.0GHz GA-P35-DS3L EVGA 8800GT Superclocked 4GB Corsair XMS2 DDR2 800 
Hard DriveHard DriveOptical DriveOS
Seagate 320GB 7200RPM Western Digital Caviar Black Lite-on DVD burner Windows 7 Professional x64 
MonitorKeyboardPowerCase
Samsung 225BW Filco Majestouch Ninja (full keyboard w/browns) Cooler Master Xtreme 650 Antec 900 
MouseAudioAudio
Logitech MX518 Denon AHD-2000 Sennheiser HD595 
  hide details  
Reply
post #5 of 8
Thread Starter 
With the code I already have I used an xml pull parser. I will post what I have later, and explain what I want it to do. I need to do some cooking soon smile.gif

But thanks to both of your for your replies
post #6 of 8
Thread Starter 
So. This is what I currently have.

This is the first xml I have to parse in order to build the app:
Warning: First xml data (Click to show)
Code:
<appinfo>
<settings>
<sessionid>some data</sessionid>
<servicesurl>a URL</servicesurl>
<settingsurl>Another URL</settingsurl>
<initialtab>Name1</initialtab>
<initialchannelid/>
<channelstab>Name2</channelstab>
<vicinitytab>Name3</vicinitytab>
<customtab>Name4</customtab>
<customtaburl>The url for the customtabsta</customtaburl>
<trackingtab>Name5</trackingtab>
<moretab>Name6</moretab>
<name>Name of the app</name>
</settings>
</appinfo>


The names: Name 1, Name 2 etc are the names for the tabs.
My main activity:
Warning: MainActivity class (Click to show)
Code:
package com.spikedreality;

import java.util.ArrayList;
import java.util.Locale;

import xmlData.StartupInfo;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.spikedreality.R;

import Network.DownloadXmlTask;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.TabHost;
import android.widget.Toast;

public class MainActivity extends SherlockFragmentActivity {
        private static final String AppInfoURL = "Containing the URL to the webservice";
        private static final String CountryId = "DK";
        private static final String LanguageId = Locale.getDefault()
                        .getDisplayLanguage();
        private static final String AppId = "AppId, that has to be hardcoded";
        private static final String DeviceId = "";
        private static final String LocalTime = "";
        private static final String Latitude = "0.000000";
        private static final String Longitude = "0.000000";
        private static final String Altitude = "0.000000";
        private static final String Device = Build.DEVICE;
        private static final String OS = "Android" + Build.VERSION.RELEASE;
        private static final String Version = "2.0.0";
        private static final String Auth = "";
        private String uniqeAppId;
        private ArrayList<StartupInfo> info;
        private String ChannelTab;
        private String VicinityTab;
        private String CustomTab;
        private String TrackingTab;
        private String MoreTab;
        public static Context appContext;
        private DownloadXmlTask downloadxml;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                String URL = CreateStartURL();
                
                try{
                        downloadxml = new DownloadXmlTask(MainActivity.this);
                        downloadxml.execute(URL);
                }catch (Exception e){
                        alert(getResources().getString(R.string.xml_error));
                }
                
                
                
                ActionBar actionbar = getSupportActionBar();
                actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                actionbar.setTitle(uniqeAppId);

                ActionBar.Tab Frag1 = actionbar.newTab().setText(ChannelTab);
                ActionBar.Tab Frag2 = actionbar.newTab().setText(VicinityTab);
                ActionBar.Tab Frag3 = actionbar.newTab().setText(CustomTab);
                ActionBar.Tab Frag4 = actionbar.newTab().setText(TrackingTab);
                ActionBar.Tab Frag5 = actionbar.newTab().setText(MoreTab);

                Fragment Fragment1 = new Channeltab();
                Fragment Fragment2 = new Vicinitytab();
                Fragment Fragment3 = new Customtab();
                Fragment Fragment4 = new Trackingtab();
                Fragment Fragment5 = new Moretab();

                Frag1.setTabListener(new MyTabListener(Fragment1));
                Frag2.setTabListener(new MyTabListener(Fragment2));
                Frag3.setTabListener(new MyTabListener(Fragment3));
                Frag4.setTabListener(new MyTabListener(Fragment4));
                Frag5.setTabListener(new MyTabListener(Fragment5));

                actionbar.addTab(Frag1);
                actionbar.addTab(Frag2);
                actionbar.addTab(Frag3);
                actionbar.addTab(Frag4);
                actionbar.addTab(Frag5);
                

        }
        
        public static String CreateStartURL() {
                String URL = "";

                URL = AppInfoURL + "Mode=" + "" + "&CountryId=" + CountryId
                                + "&LanguageId=" + LanguageId + "&AppId=" + AppId
                                + "&DeviceId=" + DeviceId + "&LocalTime=" + LocalTime
                                + "&Latitude=" + Latitude + "&Longitude=" + Longitude
                                + "&Altitude=" + Altitude + "&Device=" + Device + "&OS=" + OS
                                + "&Version=" + Version + "&Auth=" + Auth;

                return URL;

        }


        public void getTextForTabs(ArrayList<StartupInfo> info2) {
                this.info = info2;
                StartupInfo info3 = info.get(0);
                this.uniqeAppId = info3.getName();
                this.ChannelTab = info3.getChannelTab();
                this.VicinityTab = info3.getVicinityTab();
                this.CustomTab = info3.getCustomTab();
                this.TrackingTab = info3.getTrackingTab();
                this.MoreTab = info3.getMoreTab();
                
        }

        
        

        public void setInfo(ArrayList<StartupInfo> info) {
                this.info = info;

        }

        public void alert(String msg) {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

        }

        public class MyTabListener  implements ActionBar.TabListener {

                public Fragment fragment;

                public MyTabListener(Fragment fragment) {
                        this.fragment = fragment;
                }

                @Override
                public void onTabSelected(Tab tab, FragmentTransaction ft) {
                        if(fragment == null){
                                ft.add(R.id.fragment_container, fragment);
                        } else {
                                ft.attach(fragment);
                        }

                }

                @Override
                public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                        ft.remove(fragment);
                }

                @Override
                public void onTabReselected(Tab tab, FragmentTransaction ft) {

                }

        }

}

I then have a class for each fragment. Currently they all look alike. But I will show you the CustomTab, since that is by far the easiest to make, since it can be made with the first xml parsing.
Warning: Customtab class (Click to show)
Code:
package com.spikedreality;

import com.actionbarsherlock.app.SherlockFragment;
import com.example.spikedreality.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Customtab extends SherlockFragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                        return inflater.inflate(R.layout.customtabfragment, container, false);
        }

}


This customtab will only show a webpage. So it has to be a webview (this caused me alot of problems. Here is the xml for the customtab:
Warning: Customtab xml (Click to show)
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
        <WebView
        android:id="@+id/customtabfrag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
            
        </WebView>

</LinearLayout>

This is my class that is suppose to contain the startupinfo to build the app.
Warning: Startupinfoclass (Click to show)
Code:
package xmlData;

public class StartupInfo {

        public String SessionId;
        public String ServiceURL;
        public String SettingsURL;
        public String InitialTab;
        public String ChannelTab;
        public String VicinityTab;
        public String CustomTab;
        public String CustomURL;
        public String TrackingTab;
        public String MoreTab;
        public String Name;
        public String ForeColor;
        public String BackColor;
        public String VicinitySlider;

        public StartupInfo(String SessionId, String ServiceURL, String SettingsURL,
                        String InitialTab, String ChannelTab, String VicinityTab,
                        String CustomTab, String CustomURL, String TrackingTab,
                        String MoreTab, String Name, String ForeColor, String BackColor,
                        String VicinitySlider) {
                super();
                this.SessionId = SessionId;
                this.ServiceURL = ServiceURL;
                this.SettingsURL = SettingsURL;
                this.InitialTab = InitialTab;
                this.ChannelTab = ChannelTab;
                this.VicinityTab = VicinityTab;
                this.CustomTab = CustomTab;
                this.CustomURL = CustomURL;
                this.TrackingTab = TrackingTab;
                this.MoreTab = MoreTab;
                this.Name = Name;
                this.ForeColor = ForeColor;
                this.BackColor = BackColor;
                this.VicinitySlider = VicinitySlider;
        }

        public String getSessionId() {
                return SessionId;
        }

        public void setSessionId(String SessionId) {
                this.SessionId = SessionId;
        }

        public String getServiceURL() {
                return ServiceURL;
        }

        public void setServiceURL(String ServiceURL) {
                this.ServiceURL = ServiceURL;
        }

        public String getSettingsURL() {
                return SettingsURL;
        }

        public void setSettingsURL(String SettingsURL) {
                this.SettingsURL = SettingsURL;
        }

        public String getInitialTab() {
                return InitialTab;
        }

        public void setInitialTab(String InitialTab) {
                this.InitialTab = InitialTab;
        }

        public String getChannelTab() {
                return ChannelTab;
        }

        public void setChannelTab(String ChannelTab) {
                this.ChannelTab = ChannelTab;
        }

        public String getVicinityTab() {
                return VicinityTab;
        }

        public void setVicinityTab(String VicinityTab) {
                this.VicinityTab = VicinityTab;
        }

        public String getCustomTab() {
                return CustomTab;
        }

        public void setCustomTab(String CustomTab) {
                this.CustomTab = CustomTab;
        }

        public String getCustomURL() {
                return CustomURL;
        }

        public void setCustomURL(String CustomURL) {
                this.CustomURL = CustomURL;
        }

        public String getTrackingTab() {
                return TrackingTab;
        }

        public void setTrackingTab(String TrackingTab) {
                this.TrackingTab = TrackingTab;
        }

        public String getMoreTab() {
                return MoreTab;
        }

        public void setMoreTab(String MoreTab) {
                this.MoreTab = MoreTab;
        }

        public String getName() {
                return Name;
        }

        public void setName(String Name) {
                this.Name = Name;
        }

        public String getForeColor() {
                return ForeColor;
        }

        public void setForeColor(String ForeColor) {
                this.ForeColor = ForeColor;
        }

        public String getBackColor() {
                return BackColor;
        }

        public void setBackColor(String BackColor) {
                this.BackColor = BackColor;
        }

        public String getVicinitySlider() {
                return VicinitySlider;
        }

        public void setVicinitySlider(String VicinitySlider) {
                this.VicinitySlider = VicinitySlider;
        }

}

This is the downloadtask:
Warning: downloadxmltask (Click to show)
Code:
package Network;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParserException;

import xmlData.StartupInfo;

import com.actionbarsherlock.app.SherlockActivity;
import com.example.spikedreality.R;
import com.spikedreality.MainActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.webkit.WebView;
import android.widget.ListView;
import android.widget.TextView;

public class DownloadXmlTask extends AsyncTask<String, Void ,String> {
        private static boolean wifiConnected = true;
        private static boolean mobileConnected = true;
        private static final int HTTP_STATUS_OK = 500;
        private MainActivity activity;

        public DownloadXmlTask(MainActivity activity) {
                this.activity = activity;
        }


        private void showErrorPage() {
                Dialog dialog = new Dialog(null);

        }

                @Override
                protected String doInBackground(String... params) {
                        try {
                                String result = loadXml(params[0]);
                                return result;
                        } catch (IOException e) {
                                return activity.getResources().getString(R.string.connection_error);
                        } catch (XmlPullParserException e) {
                                return activity.getResources().getString(R.string.xml_error);
                        }
                }

                @Override
                protected void onPostExecute(String result) {
                        
                        
                        ArrayList<StartupInfo> info = new ArrayList<StartupInfo>();
                        if(result.length() == 0){
                                this.activity.alert("Unable to get data");
                                return;
                        }
                        try{
                        
                        String SessionId = null;
                        String ServiceURL = null;
                        String SettingsURL = null;
                        String InitialTab = null;
                        String ChannelTab = null;
                        String VicinityTab = null;
                        String CustomTab = null;
                        String CustomURL = null;
                        String TrackingTab = null;
                        String MoreTab = null;
                        String Name = null;
                        String ForeColor = null;
                        String BackColor = null;
                        String VicinitySlider = null; 
                        }
                        catch (Exception e){
                                e.printStackTrace();
                        }
                        
                }

        private String loadXml(String urlstring) throws XmlPullParserException,
                        IOException {
                InputStream stream = null;
                StartupDownload download = new StartupDownload();
                ArrayList<StartupInfo> info = new ArrayList<StartupInfo>();
                String SessionId = null;
                String ServiceURL = null;
                String SettingsURL = null;
                String InitialTab = null;
                String ChannelTab = null;
                String VicinityTab = null;
                String CustomTab = null;
                String CustomURL = null;
                String TrackingTab = null;
                String MoreTab = null;
                String Name = null;
                String ForeColor = null;
                String BackColor = null;
                String VicinitySlider = null;

                StringBuilder build = new StringBuilder();

                try {
                        stream = downloadURL(urlstring);
                        info = download.parse(stream);

                } finally {
                        if (stream != null) {
                                stream.close();
                        }
                }

                for (StartupInfo info2: info) {
                        SessionId = info2.SessionId;
                        ServiceURL = info2.ServiceURL;
                        SettingsURL = info2.SettingsURL;
                        InitialTab = info2.InitialTab;
                        ChannelTab = info2.ChannelTab;
                        VicinityTab = info2.VicinityTab;
                        CustomTab = info2.CustomTab;
                        CustomURL = info2.CustomURL;
                        TrackingTab = info2.TrackingTab;
                        MoreTab = info2.MoreTab;
                        Name = info2.Name;
                        ForeColor = info2.ForeColor;
                        BackColor = info2.BackColor;
                        VicinitySlider = info2.VicinitySlider;
                        
                        info.add(new StartupInfo(SessionId, ServiceURL, SettingsURL,
                                        InitialTab, ChannelTab, VicinityTab, CustomTab, CustomURL,
                                        TrackingTab, MoreTab, Name, ForeColor, BackColor,
                                        VicinitySlider));
                }
                return ?????;

        }

        private InputStream downloadURL(String urlstring) throws IOException {
                URL url = new URL(urlstring);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                // Starts the query
                conn.connect();
                InputStream stream = conn.getInputStream();
                return stream;

        }


}

notice that my loadxml method returns "??????". I have tried alot, but I don't know how to do it right.

and this is the parser itself:
Warning: XmlParser (Click to show)
Code:
package Network;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import xmlData.StartupInfo;

import com.example.spikedreality.R;
import com.spikedreality.MainActivity;

import commands.AppInfoCommand;
import commands.AppInfoCommand.AppInfo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Xml;

public class StartupDownload {
        private ProgressDialog progDialog;
        private Context context;
        private MainActivity activity;
        private static final String debugTag = "StartupDownload";
        private static final String ns = null;

        public ArrayList<StartupInfo> parse(InputStream in)
                        throws XmlPullParserException, IOException {
                try {
                        XmlPullParser parser = Xml.newPullParser();
                        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                        parser.setInput(in, null);
                        parser.nextTag();
                        return readFeed(parser);
                } finally {
                        in.close();
                }
        }

        private ArrayList<StartupInfo> readFeed(XmlPullParser parser)
                        throws XmlPullParserException, IOException {

                ArrayList<StartupInfo> info = new ArrayList<StartupInfo>();
                parser.require(XmlPullParser.START_TAG, ns, "appinfo");
                while (parser.next() != XmlPullParser.END_TAG) {
                        if (parser.getEventType() != XmlPullParser.START_TAG) {
                                continue;
                        }

                        String name = parser.getName();
                        if (name.equals("settings")) {
                                info.add(readInfo(parser));
                        } else {
                                skip(parser);
                        }
                }
                return info;

        }

        private StartupInfo readInfo(XmlPullParser parser)
                        throws XmlPullParserException, IOException {

                parser.require(XmlPullParser.START_TAG, ns, "settings");
                String SessionId = null;
                String ServiceURL = null;
                String SettingsURL = null;
                String InitialTab = null;
                String ChannelTab = null;
                String VicinityTab = null;
                String CustomTab = null;
                String CustomURL = null;
                String TrackingTab = null;
                String MoreTab = null;
                String Name = null;
                String ForeColor = null;
                String BackColor = null;
                String VicinitySlider = null;
                while (parser.next() != XmlPullParser.END_TAG) {
                        if (parser.getEventType() != XmlPullParser.START_TAG) {
                                continue;
                        }
                        String name = parser.getName();
                        if (name.equals("sessionid")) {
                                SessionId = readSessionId(parser);
                        } else if (name.equals("serviceurl")) {
                                ServiceURL = readServiceURL(parser);
                        } else if (name.equals("settingsurl")) {
                                SettingsURL = readSettingsURL(parser);
                        } else if (name.equals("initialtab")) {
                                InitialTab = readInitialTab(parser);
                        } else if (name.equals("channelstab")) {
                                ChannelTab = readChannelTab(parser);
                        } else if (name.equals("vicinitytab")) {
                                VicinityTab = readVicinityTab(parser);
                        } else if (name.equals("customtab")) {
                                CustomTab = readCustomTab(parser);
                        } else if (name.equals("customurl")) {
                                CustomURL = readCustomURL(parser);
                        } else if (name.equals("trackingtab")) {
                                TrackingTab = readTrackingTab(parser);
                        } else if (name.equals("moretab")) {
                                MoreTab = readMoreTab(parser);
                        } else {
                                skip(parser);
                        }
                }
                return new StartupInfo(SessionId, ServiceURL, SettingsURL, InitialTab,
                                ChannelTab, VicinityTab, CustomTab, CustomURL, TrackingTab,
                                MoreTab, Name, ForeColor, BackColor, VicinitySlider);
        }

        private String readMoreTab(XmlPullParser parser) throws IOException,
                        XmlPullParserException {
                parser.require(XmlPullParser.START_TAG, ns, "moretab");
                String MoreTab = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "moretab");
                return MoreTab;
        }

        private String readTrackingTab(XmlPullParser parser) throws IOException,
                        XmlPullParserException {
                parser.require(XmlPullParser.START_TAG, ns, "trackingtab");
                String TrackingTab = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "trackingtab");
                return TrackingTab;
        }

        private String readCustomURL(XmlPullParser parser) throws IOException,
                        XmlPullParserException {
                parser.require(XmlPullParser.START_TAG, ns, "customurl");
                String CustomURL = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "customurl");
                return CustomURL;
        }

        private String readCustomTab(XmlPullParser parser) throws IOException,
                        XmlPullParserException {
                parser.require(XmlPullParser.START_TAG, ns, "customtab");
                String CustomTab = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "customtab");
                return CustomTab;
        }

        private String readVicinityTab(XmlPullParser parser) throws IOException,
                        XmlPullParserException {
                parser.require(XmlPullParser.START_TAG, ns, "vicinitytab");
                String VicinityTab = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "vicinitytab");
                return VicinityTab;
        }

        private String readChannelTab(XmlPullParser parser)
                        throws XmlPullParserException, IOException {
                parser.require(XmlPullParser.START_TAG, ns, "channelstab");
                String ChannelTab = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "channelstab");
                return ChannelTab;
        }

        private String readInitialTab(XmlPullParser parser)
                        throws XmlPullParserException, IOException {
                parser.require(XmlPullParser.START_TAG, ns, "initialtab");
                String InitialTab = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "initialtab");
                return InitialTab;
        }

        private String readSettingsURL(XmlPullParser parser)
                        throws XmlPullParserException, IOException {
                parser.require(XmlPullParser.START_TAG, ns, "settingsurl");
                String SettingsURL = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "settingsurl");
                return SettingsURL;
        }

        private String readServiceURL(XmlPullParser parser)
                        throws XmlPullParserException, IOException {
                parser.require(XmlPullParser.START_TAG, ns, "serviceurl");
                String ServiceURL = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "serviceurl");
                return ServiceURL;
        }

        private String readSessionId(XmlPullParser parser)
                        throws XmlPullParserException, IOException {
                parser.require(XmlPullParser.START_TAG, ns, "sessionid");
                String SessionId = readText(parser);
                parser.require(XmlPullParser.END_TAG, ns, "sessionid");
                return SessionId;
        }

        private String readText(XmlPullParser parser)
                        throws XmlPullParserException, IOException {
                String result = "";
                if (parser.next() == XmlPullParser.TEXT) {
                        result = parser.getText();
                        parser.nextTag();
                }
                return result;
        }

        private void skip(XmlPullParser parser) throws XmlPullParserException,
                        IOException {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                        throw new IllegalStateException();
                }
                int depth = 1;
                while (depth != 0) {
                        switch (parser.next()) {
                        case XmlPullParser.END_TAG:
                                depth--;
                                break;
                        case XmlPullParser.START_TAG:
                                depth++;
                                break;
                        }
                }
        }
}

The problem I have. The xml I have to parser in the beginning contains the name for the tabs, and the info that i need to make new calls to the webservice to recieve more xml to create content for the other tabs. I however cannot get access to the things I try to parse. And I can't really see why not ^^
Edited by Panzerfury - 7/24/13 at 5:42pm
post #7 of 8
Quote:
Originally Posted by Panzerfury View Post

So. This is what I currently have.

This is the first xml I have to parse in order to build the app:
Warning: First xml data (Click to show)
Code:
<settings>
<appinfo>
<sessionid>some data</sessionid>
<servicesurl>a URL</servicesurl>
<settingsurl>Another URL</settingsurl>
<initialtab>Name1</initialtab>
<initialchannelid/>
<channelstab>Name2</channelstab>
<vicinitytab>Name3</vicinitytab>
<customtab>Name4</customtab>
<customtaburl>The url for the customtabsta</customtaburl>
<trackingtab>Name5</trackingtab>
<moretab>Name6</moretab>
<name>Name of the app</name>
</settings>
</appinfo>

Your first problem is that xml is mal-formed.
The setting and appinfo tags are out of order.
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #8 of 8
Thread Starter 
Quote:
Originally Posted by wedge View Post

Your first problem is that xml is mal-formed.
The setting and appinfo tags are out of order.

The xml is mal-formed ? What do you mean by that ? I altered some names. But the tags should be what I get when I make a call to the webservice

EDIT: I see what you mean. The appinfo is suppose to be at the top. Not settings... That's my editing error, i'll just correct it.

EDIT2: It is now corrected
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Looking for android programmer to help me get started with an app