0

I have recently developed an android app, which is parsing the xml links, whenever i use the app it responds very slowly, because each time i open the app and navigate the pages on the app, it parse it then give the data,

is there any way so that i could speed up the app, i have searched for it on google, but have not got any idea, it stated to use the volley library, but no result as i am unable to use that,

plz help guys if anyone has any option , is there any way that i could parse the link and store it into the cache and then retrive from there..

package com.stepupup.pm;

import info.stepupup.tabswipe.R;

 import java.util.ArrayList;
 import java.util.HashMap;

   import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;
 import android.content.Intent;
import android.graphics.Typeface;
 import android.os.Bundle;
  import android.support.v4.app.ListFragment;
  import android.view.LayoutInflater;
  import android.view.View;
   import android.view.ViewGroup;
   import android.widget.AdapterView;
   import android.widget.ListAdapter;
   import android.widget.ListView;
   import android.widget.SimpleAdapter;
   import android.widget.TextView;
   import android.widget.AdapterView.OnItemClickListener;

  public class Cricket extends ListFragment {

static final String URL = "http://www.statenewsonline.com/category/sports/cricket/feed";
// XML node keys
static final String KEY_ITEM = "item"; // parent node

static final String KEY_NAME = "title";
static final String KEY_COST = "link";
static final String KEY_DESC = "content:encoded";
static final String KEY_PUB = "pubDate";
Context context;
Typeface font;
String nam,cost,desc,ti,cdesc,lnk1;
TextView t1;;
TextView t2,t3,t5,t15;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.cricket, container, false);
    font=Typeface.createFromAsset(getActivity().getAssets(), "DroidHindi.ttf");
    return rootView;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


     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_NAME, (parser.getValue(e, KEY_NAME)));
            map.put(KEY_COST, parser.getValue(e, KEY_COST));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
            map.put(KEY_PUB, parser.getValue(e, KEY_PUB));
    //  map.put(KEY_CDESC, parser.getValue(e, KEY_CDESC));

            // adding HashList to ArrayList
            menuItems.add(map);
        }
        ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC,KEY_COST}, new int[] {
                        R.id.name1, R.id.description, R.id.link1}){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewGroup view = (ViewGroup) super.getView(position, convertView, parent);
            if(convertView == null) Cricket.setAppFont(view, font);
            t1=(TextView)view.findViewById(R.id.name1);


            t1.setTypeface(font);

             t2=((TextView)view.findViewById(R.id.description));

            t2.setTypeface(font);

            t15=(TextView)view.findViewById(R.id.link1);

            t15.setTypeface(font);

            return view;
        }
    };

    setListAdapter(adapter);
    // selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // getting values from selected ListItem

         t1=(TextView)view.findViewById(R.id.name1);
            nam=t1.getText().toString();
             t2=((TextView)view.findViewById(R.id.description));
             desc = t2.getText().toString();

             t15=(TextView)view.findViewById(R.id.link1);

             lnk1=t15.getText().toString();
        // cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
         ti = ((TextView) view.findViewById(R.id.time)).getText().toString();






        // Starting new intent
        Intent in = new Intent(getActivity(), SingleMenuItemActivity.class);
        in.putExtra(KEY_NAME, nam);
        in.putExtra(KEY_COST, lnk1);
        in.putExtra(KEY_DESC, desc);
        in.putExtra(KEY_PUB, ti);

        startActivity(in);

    }
});




   }
     protected static void setAppFont(ViewGroup view, Typeface font2) {
         // TODO Auto-generated method stub

     }



   }

and the folllwing is my xml file..

     <?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"
    android:background="#ffffff" >

     <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</LinearLayout>
Mirnal Mannu
  • 93
  • 1
  • 10
  • 1
    If you are parsing in UI thread, why don't use [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) to parse in background. – itechevo Apr 15 '15 at 10:45
  • i am using the AsyncTask, I have some pages on my app ( used view pager ) which when slided the page changes, each time i slide the page it lags and loads very slowly, even i slided a page, and within seconds came back to that page it again loads, which makes my app very slow – Mirnal Mannu Apr 15 '15 at 11:03

2 Answers2

1

Android Documentation says,

We recommend XmlPullParser, which is an efficient and maintainable way to parse XML on Android. Historically Android has had two implementations of this interface:

  1. KXmlParser via XmlPullParserFactory.newPullParser().
  2. ExpatPullParser, via Xml.newPullParser().

You can find a good example in that page. Don't forget to use Async task and do that network related stuff in background. Good luck!

Yuva Raj
  • 3,881
  • 1
  • 19
  • 30
  • thanks for your help, but i am not getting into that whatever you have said, as i am new to android ... can you help me if i give you my class.java file, that how to use this in the coding. plz. – Mirnal Mannu Apr 15 '15 at 11:06
  • Check out my post. I created as a Java project, but it almost same for android too. The code after main function. http://stackoverflow.com/questions/28373487/getting-started-with-xmlpullparser/28383295#28383295 & this too http://stackoverflow.com/questions/29442422/android-parsing-xml-to-textview/29581873#29581873 – Yuva Raj Apr 15 '15 at 11:07
  • i have updated my question with the coding, can you please help me that what i have to do, just do a favour for me, i am new to android, if you can help then plz. edit my code with the required necessary codings, such that the app becomes fast. – Mirnal Mannu Apr 15 '15 at 11:20
0

One obvious problem with your code is that you do network request on the UI thread, here:

public void onActivityCreated(Bundle savedInstanceState) {
    ...
    String xml = parser.getXmlFromUrl(URL); // getting XML
    ...
}

I don't know where XMLParser is coming from, but, just from the method's name, it is downloading something from the network. You should never do such things on the UI thread. Instead, this should be done on the background thread, and the results should be delivered on the UI thread.

Now which exactly mechanism to use for that is up to you. There are lots of options, with different advantages and trade-offs. AsyncTask mechanism was already mentioned, and it indeed might be an appropriate solution (but not always). Other possibilities are RxJava, RoboSpice, Guava's ListenableFutures, etc.

Haspemulator
  • 11,050
  • 9
  • 49
  • 76
  • so now what to do for that. – Mirnal Mannu Apr 15 '15 at 12:20
  • What to do? Well, I'd start with familiarizing myself with the libraries and classes described and tried to choose which one suits my needs best. Then I'd applied the best one and checked how it works. If I'd still have problems, I'd posted another question here (or looked for existing one describing the same problem). If you're expecting someone to write the code for you, you better check oDesk or RentACoder. – Haspemulator Apr 15 '15 at 12:24
  • can you help me that how to deal with that line, and how can i remove the loading on ui thread, any alternate option for that – Mirnal Mannu Apr 15 '15 at 12:45