How to read XML response from a URL in java and return as String
Sol:
public String getXXX(String feedId){
String sql = null;
String UrlString =null;
String output = "";
try{
sql = AppUtil.getProperty("Q_GET_FEED");
Formatter fmt = new Formatter();
sql = fmt.format(sql, feedId).toString();
List l = (List)AppUtil.runAQueryCurrentSession(sql,Feeder.class);
Feeder feeder = (Feeder) l.get(0);
UrlString = feeder.getLink();
// open url connection
URL url = new URL(UrlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// set up url connection to get retrieve information back
con.setRequestMethod("GET");
con.setDoInput(true);
// pull the information back from the URL
InputStream is = con.getInputStream();
StringBuffer buf = new StringBuffer();
int c;
while ((c = is.read()) != -1) {
buf.append((char) c);
}
con.disconnect();
output = buf.toString();
// Just throw the output from the URL
return output;
}catch(Exception e){
e.printStackTrace();
log.error(" Error in retriveing feed data"
+ e.getMessage());
return "";
}
}
Sol:
public String getXXX(String feedId){
String sql = null;
String UrlString =null;
String output = "";
try{
sql = AppUtil.getProperty("Q_GET_FEED");
Formatter fmt = new Formatter();
sql = fmt.format(sql, feedId).toString();
List l = (List)AppUtil.runAQueryCurrentSession(sql,Feeder.class);
Feeder feeder = (Feeder) l.get(0);
UrlString = feeder.getLink();
// open url connection
URL url = new URL(UrlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// set up url connection to get retrieve information back
con.setRequestMethod("GET");
con.setDoInput(true);
// pull the information back from the URL
InputStream is = con.getInputStream();
StringBuffer buf = new StringBuffer();
int c;
while ((c = is.read()) != -1) {
buf.append((char) c);
}
con.disconnect();
output = buf.toString();
// Just throw the output from the URL
return output;
}catch(Exception e){
e.printStackTrace();
log.error(" Error in retriveing feed data"
+ e.getMessage());
return "";
}
}
No comments:
Post a Comment