Wednesday, September 14, 2011

Asyn Parser

Step 1:

package com.parser;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


public abstract class BaseFeedParser implements FeedParser {

private final URL feedUrl1;

protected BaseFeedParser(String feedUrl){
try {
this.feedUrl1 = new URL(feedUrl);
} catch (MalformedURLException e) {
throw new RuntimeException("BasefeedParser wrong url /n"+e);
}
}

protected InputStream getInputStream() {
try {
return feedUrl1.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException("connection failed IO exp "+e);
}

}
}




Step 2:




import java.util.List;

public interface FeedParser {


List parse();

}


Step 3:


public class FeedParserFactory {

static String feedUrl = "";
public FeedParserFactory(String url){

feedUrl=url;
}
public FeedParser getParser(){
return getParser(PARSERType.XML_HOME);
}

public FeedParser getParser(PARSERTypetype){
switch (type) {

case 1:
return new XmlParser(feedUrl);

default:
return null;
}

}
}

Step 4:

public enum PARSERType{

1;

}

Step 5:


public class message {

private String 1="";

public String get1(){
return 1;
}
public void set1(String 1){
this.1=1;
}

Step 6:

package com.parser;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;

public class XmlParserHomePage extends BaseFeedParser {

public XmlParserHomePage(String feedUrl) {
// TODO Auto-generated constructor stub
super(feedUrl);
}
public List parse() {
// TODO Auto-generated method stub
List messages = null;
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(getInputStream(), null);
int eventType = parser.getEventType();
message currentMessage = null;
boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
messages = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("1")){
currentMessage = new message();
} else if (currentMessage != null){
if(name.equalsIgnoreCase("2")){
currentMessage.set1(parser.nextText());
}else if(name.equalsIgnoreCase("3")||name.equalsIgnoreCase("4")){
currentMessage.set2(parser.nextText());
}else if(name.equalsIgnoreCase("5")||name.equalsIgnoreCase("6")){
currentMessage.set3(parser.nextText());
}else if(name.equalsIgnoreCase("distance")){
currentMessage.set4o(parser.nextText());
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("1")&& currentMessage != null){
messages.add(currentMessage);
}
else if(name.equalsIgnoreCase("0")){
done = true;
}
break;
}
eventType = parser.next();
}
} catch (Exception e) {
Log.e("Error Mian PullFeedParser", e.getMessage(), e);
//throw new RuntimeException(e);

}
//return messages;
System.out.println("Pass ctrl 2 Main");
return messages;

}

@Override
public List parsecoupen() {
// TODO Auto-generated method stub
return null;
}


}


Step 7:

private void loadparser(PARSERType type) {

try{

new DataProviderCoupon().execute(type);

}catch(Exception e){
ProgressCancel();
System.out.println("Error in Main Load feed method");
}
}

class DataProviderCoupon extends AsyncTask<>{

@Override
protected List doInBackground(PARSERType... params) {

FeedParserFactory f=new FeedParserFactory(url);
FeedParser parser = f.getParser(params[0]);
List messages=parser.parse();

return messages;
}
@Override
protected void onPostExecute(List messages) {

try{
}

catch(Exception e){

System.out.println("Error in getting data...."+e);

}

super.onPostExecute(messages);
}
}


final Handler mHandler = new Handler();

final Runnable mUpdateResults = new Runnable() {
public void run() {

updateUIwithData();

}
};

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. class DataProviderCoupon extends AsyncTask>{

    ReplyDelete