//Sample example for using httpclient and firing a getmethod and parsing the response with abdera parser
public class testUrl {
public static void main(String args[]){
String sourceMethod = "getMessageBoard";
Response response = null;
ResponseBuilder builder = null;
System.out.println("entering ");
try {
HttpClient client = new HttpClient();//getting httpclient object
GetMethod method = new GetMethod("your feed url for getting blogs xml of connections or any feed url"); //preparing the get method
int status = client.executeMethod(method); //firing the get url
System.out.println("Get status is: " + status); //printing the status
String responseData = method.getResponseBodyAsString();
System.out.println("Response Data is: " + responseData); //printing the response stream
Parser parser = Abdera.getNewParser(); //getting parser object
try {
InputStream in = method.getResponseBodyAsStream();//geting response data as stream and parsing it
Document<Feed> doc = parser.parse(in); //passing inputstream to parse function
JSONArray data = FeedParser.atomparse(doc);// parse function is given bellow
JSONObject finalJsonObject = new JSONObject();
finalJsonObject.put("items", data);
String responseString = finalJsonObject.toString();
builder = Response.ok(responseString , MediaType.APPLICATION_JSON);//this is useful for making your application as rest other wise you can omit this line and bellow line
response = builder.build(); //you can send this response using rest api or send to jsp for printing by iterating
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Bellow is the parser class for getting json array of required content from blogs xml by parsing it
public class FeedParser {
public static JSONArray atomparse(Document<Feed> commDom) {
JSONArray blogList = null;
try {
blogList = new JSONArray();
Feed feed = commDom.getRoot();
List<Entry> requiredEntries = feed.getEntries();
for (Entry entry : requiredEntries) {
JSONObject blog = new JSONObject();
// System.out.println("-- starting of entry --");
List<Element> elements = entry.getElements();
for (Element allEntries : elements) {
if (allEntries.getQName().getLocalPart().equals("link")) {
String attrValue = allEntries.getAttributeValue("rel");
if (attrValue.equals("alternate")) {
System.out.println("link -> "
+ allEntries.getAttributeValue("href"));
blog.put("link",
allEntries.getAttributeValue("href"));
}
}
if (allEntries.getQName().getLocalPart().equals("rank")) {
System.out.println("snx:rank -> " +
allEntries.getText());
System.out.println("individual scheme values"+allEntries.getAttributeValue("scheme"));
if(allEntries.getAttributeValue("scheme").contains("hit")){
System.out.println("number of visits"+allEntries.getText());
blog.put("Visits",allEntries.getText());
}
else if(allEntries.getAttributeValue("scheme").contains("comment")){
System.out.println("number of comments"+allEntries.getText());
blog.put("Comments",allEntries.getText());
}
//blog.put("summary", allEntries.getText());
}
if (allEntries.getQName().getLocalPart().equals("summary")) {
// System.out.println("summary -> " +
// allEntries.getText());
blog.put("summary", allEntries.getText());
}
if (allEntries.getQName().getLocalPart().equals("updated")) {
String reqTime = allEntries.getText();//parsing date
String dateString = reqTime.substring(0,
reqTime.indexOf('T'));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
String reqDate = "";
String converted = "";
try {
Date convertedDate = dateFormat.parse(dateString);
reqDate = convertedDate.toString();
converted = reqDate.substring(3,
reqDate.indexOf(':') - 3);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
blog.put("updated", converted);
}
if (allEntries.getQName().getLocalPart().equals("title")) {
// System.out.println("title -> " +
// allEntries.getText());
blog.put("title", allEntries.getText());
}
if (allEntries.getQName().getLocalPart().equals("author")) {
List<Element> objectElements = allEntries.getElements();
for (Element allElements : objectElements) {
if (allElements.getQName().getLocalPart()
.equals("name")) {
// System.out.println("author -> "
// + allElements.getText());
blog.put("author", allElements.getText());
}
}
}
}
// System.out.println("-- ending of entry --");
blogList.add(blog);
}
} catch (Exception e) {
System.out.println(e);
}
return blogList;
// End of Function
}
}
No comments:
Post a Comment