0

I am trying to do an XML Parsing by getting a token and implementing them to my databases which I`ve created through them at SQLite database. This is a coffee shop order app I should parse the data I got through the token and implement the data (table_id, table_status, product_id, product_title, product_price) into my databases so they will Now I have two tables "products" and "tables" even though I got the token from the CTower which is "429925" and have an output like this

"3-OK 1410 1420 1430 1440 1450 1460 1470 1480 1490 14 2.00 9 3.00 13 1.50 5 1.50 2 3.00 1 2.50 7 4.00 6 1.50 12 2.50 15 4.00 3 2.00 4 2.50 16 4.00 10 3.00 8 3.50 11 3.00"

I should imply this into the tables ("products" and "tables") and have an outcome like:

<?xml version="1.0" encoding="utf-8"?>
<response>
<status>3-OK</status>
<msg>
<tables>
<table>
<id>501</id>
<status>1</status>
</table>
<table>
<id>502</id>
<status>0</status>
</table>
……….
</tables>
<products>
<product>
<id>14</id>
<title>Capuccino</title>
<price>2.00</price>
</product>
<product>
<id>9</id>
<title>Chamomile</title>
<price>3.00</price>
</product>
……….
</products>
</msg>
</response>

I tried the code below and many others but can not make the parsing apparently, can you detect what seems wrong here? Please ignore the 0-fail,0-ok,1-fail,1-ok,2-fail,3-ok,3-fail parts they are for another activity. I would appreciate any help, thank you!

package android.madclassproj;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

import android.database.sqlite.SQLiteDatabase;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;


public class RemoteContent extends AsyncTask<String, Void, String> {
    private String ResponseStatus;
    private String ResponseMessage;
    private SQLiteDatabase myDB;

    public String id;
    public String status;
    public String color;
    public String title;
    public String price;

    private Context CallingContext;


    RemoteContent(Context ct) {
        super();
        CallingContext = ct;
    }

    /// Background download of the page.
    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        try {
            URL url = new URL(urls[0]);
            try {
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                    response = readStream(in);
                } finally {
                    urlConnection.disconnect();
                }
            } catch (java.io.IOException ioex) {
                System.out.println("Exception Catched: Java IO");
            }
        } catch (MalformedURLException muex) {
            System.out.println("Exception Catched : Malformed URL");
        }
        return response;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// Determine the application's actions according to the data sent by the control tower ///////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    protected void onPostExecute(String result) {

        ResponseStatus = getXMLContent(result, "<status>", "</status>", 0);
        ResponseMessage = getXMLContent(result, "<msg>", "</msg>", 0);

        if (ResponseStatus.equals("0-FAIL")) {
            System.out.println("HERE HERE");
            Toast.makeText(CallingContext, R.string.auth_fail, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(CallingContext, LoginActivity.class);
            CallingContext.startActivity(intent);

        } else if (ResponseStatus.equals("0-OK")) {
            Toast.makeText(CallingContext, R.string.auth_ok, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(CallingContext, MenuActivity.class);
            Bundle b = new Bundle();
            b.putString("msg", ResponseMessage);
            intent.putExtras(b);
            CallingContext.startActivity(intent);

        } else if (ResponseStatus.equals("1-FAIL")) {
            Toast.makeText(CallingContext, R.string.auth_fail, Toast.LENGTH_SHORT).show();
            ((LoginActivity)CallingContext).text_auth_fail.setText(ResponseMessage);
            ((LoginActivity)CallingContext).text_auth_fail.setVisibility(View.VISIBLE);

        } else if (ResponseStatus.equals("1-OK")) {
            ((LoginActivity)CallingContext).text_auth_fail.setVisibility(View.INVISIBLE);
            ((LoginActivity)CallingContext).text_auth_ok.setText(ResponseMessage);
            ((LoginActivity)CallingContext).text_auth_ok.setVisibility(View.VISIBLE);
            ((LoginActivity)CallingContext).text_instr1.setVisibility(View.INVISIBLE);
            ((LoginActivity)CallingContext).edit_email.setVisibility(View.INVISIBLE);
            ((LoginActivity)CallingContext).img_btn.setVisibility(View.INVISIBLE);

        } else if (ResponseStatus.equals("2-OK")) {
            try {
                System.out.println(" ====== Trying....");
                MediaPlayer rmp = ((JukeboxActivity) CallingContext).MP;

                String song_artist = getXMLContent(ResponseMessage, "<artist>", "</artist>", 0);
                String song_title = getXMLContent(ResponseMessage, "<title>", "</title>", 0);
                String song_url = getXMLContent(ResponseMessage, "<url>", "</url>", 0);

                rmp.setDataSource(song_url);
                rmp.prepare();
                rmp.start();

                ((JukeboxActivity) CallingContext).tv_artist.setText(song_artist);
                ((JukeboxActivity) CallingContext).tv_title.setText(song_title);
                ((JukeboxActivity) CallingContext).tv_url.setText(song_url);

                ((JukeboxActivity) CallingContext).btn_get.setEnabled(true);
                ((JukeboxActivity) CallingContext).btn_get.setBackgroundColor(Color.parseColor("#99cc00"));
                ((JukeboxActivity) CallingContext).btn_play.setEnabled(false);
                ((JukeboxActivity) CallingContext).btn_play.setBackgroundColor(Color.GRAY);
                ((JukeboxActivity) CallingContext).btn_pause.setEnabled(true);
                ((JukeboxActivity) CallingContext).btn_pause.setBackgroundColor(Color.parseColor("#ff4444"));

                ((JukeboxActivity) CallingContext).tv_status.setText(R.string.playing);

            } catch (Exception e) {
                System.out.println(" ====== Exception catched");
                e.printStackTrace();
            }


        } else if (ResponseStatus.equals("3-FAIL")) {
            ResponseStatus = getXMLContent(result, "<table_status>", "</table_status>", 0);
            ResponseMessage = getXMLContent(result, "<table_id>", "</table_id>", 0);
            String table_status = getXMLContent(ResponseMessage, "<table_status>", "</table_status>", 0);
            String table_id = getXMLContent(ResponseMessage, "<table_id>", "</table_id>", 0);
            ((TablesActivity) CallingContext).button1.setBackgroundColor(Color.parseColor("GREEN"));


        } else if (ResponseStatus.equals("3-OK")) {
            ResponseStatus = getXMLContent(result, "<table_status>", "</table_status>", 0);
            ResponseMessage = getXMLContent(result, "<table_id>", "</table_id>", 0);
            String table_status = getXMLContent(ResponseMessage, "<table_status>", "</table_status>", 0);
            String table_id = getXMLContent(ResponseMessage, "<table_id>", "</table_id>", 0);
            ((TablesActivity) CallingContext).button1.setBackgroundColor(Color.parseColor("RED"));
            
            XmlPullParserFactory pullParserFactory;
            try {
                pullParserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = pullParserFactory.newPullParser();

                InputStream in_s = CallingContext.getApplicationContext().getAssets().open("temp.xml");
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in_s, null);

               

            } catch (XmlPullParserException e) {

                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    private <product> void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
    {
        ArrayList<product> products = null;
        int eventType = parser.getEventType();
        Parser currentParser = null;

        while (eventType != XmlPullParser.END_DOCUMENT){
            String name = null;
            switch (eventType){
                case XmlPullParser.START_DOCUMENT:
                    products = new ArrayList();
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name == "product"){
                        currentParser = new Parser();
                    } else if (currentParser != null){
                        if (name == "id"){
                            currentParser.id = parser.nextText();
                        } else if (name == "title"){
                            currentParser.title = parser.nextText();
                        } else if (name == "price"){
                            currentParser.price= parser.nextText();
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("products") && currentParser != null){
                        products.add((product) currentParser);
                    }
            }
            eventType = parser.next();
        }

        printProducts(products);
    }


    private void printProducts(ArrayList products)
    {
        String content = "";
        Iterator it = products.iterator();
        while(it.hasNext())
        {
            Parser currProduct  = (Parser) it.next();
            content = content + "id :" +  currProduct.id + "n";
            content = content + "title :" +  currProduct.title + "n";
            content = content + "price :" +  currProduct.price + "n";

        }

        TextView display = (TextView)findViewById(R.id.info);
        display.setText(content);
    }

    private void databaseHelper(RemoteContent remoteContent) {

    }

    private String readStream(InputStream is) {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            int i = is.read();
            while(i != -1) {
                bo.write(i);
                i = is.read();
            }
            return bo.toString();
        } catch (IOException e) {
            return "";
        }
    }

    private String getXMLContent(String str, String XMLOpen, String XMLClose, int start) {
        String content = "";

        /// Get the message
        int p1 = str.indexOf(XMLOpen, start);
        int p2 = str.indexOf(XMLClose, p1);
        if (p1 > 0 && p2 > p1) {
            p1 += XMLOpen.length();
            content = str.substring(p1, p2);
        }
        return content;
    }


    private void parseXML(XmlPullParser parser) {
    }


    public String getResponseStatus() { return ResponseStatus; }
    public String getResponseMessage() { return ResponseMessage; }
}
Ruken K
  • 3
  • 2

0 Answers0