package com.ram.parser;
public class FeedParserFactory {
static String feedUrl = "";
public FeedParserFactory(String url){
feedUrl=url;
}
public FeedParser getParser(){
return getParser(ParserType.XML_PULL);
}
public FeedParser getParser(ParserType type){
switch (type) {
case XML_PULL:
return new XmlPullFeedParser(feedUrl);
default:
return null;
}
}
}
step-4:
package com.ram.parser;
import java.util.List;
public interface FeedParser {
void parse(ParserCallback details);
}
step-2:
package com.ram.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 {
System.out.println("url-- "+feedUrl1);
return feedUrl1.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException("connection failed IO exp "+e);
}
}
}
step-5:
package com.ram.parser;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Log;
import android.util.Xml;
public class XmlPullFeedParser extends BaseFeedParser {
ParserCallback callback;
public XmlPullFeedParser(String feedUrl) {
super(feedUrl);
}
public void parse(final ParserCallback callback) {
this.callback = callback;
new Thread(){
@Override
public void run(){
boolean keepGoing = true;
while(keepGoing){
List
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(getInputStream(), null);
System.out.println("Parser Data--------------\n------"+parser.getText());
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("node")){
currentMessage = new message();
} else if (currentMessage != null){
if (name.equalsIgnoreCase("teams")){
currentMessage.setover(parser.nextText());
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("node")){
messages.add(currentMessage);
}
else if(name.equalsIgnoreCase("xml")){
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");
callback.parseDidFinish(messages);
keepGoing = false;
}
}
}.start();
}
@Override
public List
// TODO Auto-generated method stub
return null;
}
}
step-6:
package com.ram.parser;
import java.util.List;
public interface ParserCallback {
public void parseDidFinish(List
}
step-7:
package com.ram.parser;
/**
*
*/
public enum ParserType{
XML_PULL;
}
No comments:
Post a Comment