You need to sign in to do that
Don't have an account?
Trinath Reddy
Need to change http response to HTML format
Please check my below code and suggest for the same.
Page:
<apex:page standardController="FeedLinks__c" extensions="JSONParserUtil" showheader="false" sidebar="false">
<apex:pageBlock title="Feed News">
<apex:pageMessages /><apex:outputText value="{!responsemsg}" />
</apex:pageBlock>
</apex:page>
Controller:
public class JSONParserUtil {
public String jsonvalue { get; set; }
public List<FeedLinks__c> lstlink{get;set;}
public String responsemsg { get; set; }
public JSONParserUtil(ApexPages.StandardController controller) {
responsemsg='';
this.parseJSONResponse();
}
public void parseJSONResponse() {
Http httpProtocol = new Http();
// Create HTTP request to send.
HttpRequest request = new HttpRequest();
// Set the endpoint URL.
String endpoint = 'https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=89edf61c78424b0a8dcafc4f9739f031';
request.setEndPoint(endpoint);
// Set the HTTP verb to GET.
request.setMethod('GET');
// Send the HTTP request and get the response.
// The response is in JSON format.
HttpResponse response = httpProtocol.send(request);
System.debug(response.getBody());
if (response.getStatusCode() == 200) {
JSONParser parser = JSON.createParser(response.getBody());
system.debug('Parser='+parser);
lstlink=new List<FeedLinks__c>();
while (parser.nextToken() != null) {
responsemsg+= parser.gettext();
parser.nextToken();
}
system.debug('response msg='+responsemsg);
}
}
public class jsonvalue{
public String title{get;set;}
Public String description {get;set;}
}
}
I am geeting Http response like:
{"status":"ok","source":"the-next-web","sortBy":"latest","articles":[{"author":"Rachel Kaser","title":"Donuts delivered via drone is the future we all imagined","description":"This Wednesday, a Denver-based bakery, in collaboration with Texas-based company Drone Dispatch, delivered fresh donuts to the Denver city officials via drone. Recipients include policemen, ...","url":"https://thenextweb.com/insider/2017/06/01/donuts-delivered-via-drone-future-imagined/","urlToImage":"https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2017/06/Drone-donuts.jpg","publishedAt":"2017-06-01T03:13:11Z"},
I am geeting Vf page output like:
{okthe-next-weblatest[authortitledescriptionurlurlToImagepublishedAt}
Excepted output format should be like;
tille:Donuts delivered via drone is the future we all imagined
description:This Wednesday, a Denver-based bakery, in collaboration with Texas-based company Drone Dispatch, delivered fresh donuts to the Denver city officials via drone. Recipients include policemen
url:https://thenextweb.com/insider/2017/06/01/donuts-delivered-via-drone-future-imagined/
publishedAt:2017-06-01T03:13:11Z
Page:
<apex:page standardController="FeedLinks__c" extensions="JSONParserUtil" showheader="false" sidebar="false">
<apex:pageBlock title="Feed News">
<apex:pageMessages /><apex:outputText value="{!responsemsg}" />
</apex:pageBlock>
</apex:page>
Controller:
public class JSONParserUtil {
public String jsonvalue { get; set; }
public List<FeedLinks__c> lstlink{get;set;}
public String responsemsg { get; set; }
public JSONParserUtil(ApexPages.StandardController controller) {
responsemsg='';
this.parseJSONResponse();
}
public void parseJSONResponse() {
Http httpProtocol = new Http();
// Create HTTP request to send.
HttpRequest request = new HttpRequest();
// Set the endpoint URL.
String endpoint = 'https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=89edf61c78424b0a8dcafc4f9739f031';
request.setEndPoint(endpoint);
// Set the HTTP verb to GET.
request.setMethod('GET');
// Send the HTTP request and get the response.
// The response is in JSON format.
HttpResponse response = httpProtocol.send(request);
System.debug(response.getBody());
if (response.getStatusCode() == 200) {
JSONParser parser = JSON.createParser(response.getBody());
system.debug('Parser='+parser);
lstlink=new List<FeedLinks__c>();
while (parser.nextToken() != null) {
responsemsg+= parser.gettext();
parser.nextToken();
}
system.debug('response msg='+responsemsg);
}
}
public class jsonvalue{
public String title{get;set;}
Public String description {get;set;}
}
}
I am geeting Http response like:
{"status":"ok","source":"the-next-web","sortBy":"latest","articles":[{"author":"Rachel Kaser","title":"Donuts delivered via drone is the future we all imagined","description":"This Wednesday, a Denver-based bakery, in collaboration with Texas-based company Drone Dispatch, delivered fresh donuts to the Denver city officials via drone. Recipients include policemen, ...","url":"https://thenextweb.com/insider/2017/06/01/donuts-delivered-via-drone-future-imagined/","urlToImage":"https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2017/06/Drone-donuts.jpg","publishedAt":"2017-06-01T03:13:11Z"},
I am geeting Vf page output like:
{okthe-next-weblatest[authortitledescriptionurlurlToImagepublishedAt}
Excepted output format should be like;
tille:Donuts delivered via drone is the future we all imagined
description:This Wednesday, a Denver-based bakery, in collaboration with Texas-based company Drone Dispatch, delivered fresh donuts to the Denver city officials via drone. Recipients include policemen
url:https://thenextweb.com/insider/2017/06/01/donuts-delivered-via-drone-future-imagined/
publishedAt:2017-06-01T03:13:11Z
I changed the code try using this. if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
This will help keep the forum future users determine what answers are useful
VF Page
<apex:page standardController="contact" extensions="JSONParserUtil" showheader="false" sidebar="false">
<apex:pageBlock title="Feed News">
<apex:pageMessages />
<apex:pageBlockTable var="a" value="{!wrapperlist}">
<apex:column value="{!a.author}"/>
<apex:column value="{!a.title}"/>
<apex:column value="{!a.description}"/>
<apex:column value="{!a.url}"/>
<apex:column value="{!a.urlToImage}"/>
<apex:column value="{!a.publishedAt}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Apex Class
public class JSONParserUtil {
public List<ProWrapper> wrapperlist{get;set;}
public JSONParserUtil(ApexPages.StandardController controller) {
this.parseJSONResponse();
}
public void parseJSONResponse() {
Http httpProtocol = new Http();
// Create HTTP request to send.
HttpRequest request = new HttpRequest();
// Set the endpoint URL.
String endpoint = 'https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=89edf61c78424b0a8dcafc4f9739f031';
request.setEndPoint(endpoint);
// Set the HTTP verb to GET.
request.setMethod('GET');
// Send the HTTP request and get the response.
// The response is in JSON format.
HttpResponse response = httpProtocol.send(request);
System.debug('====Resp======'+response.getBody());
if (response.getStatusCode() == 200) {
wrapperlist = new List<ProWrapper>();
JSONParser parser = JSON.createParser(response.getBody());
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
Jsonparsercls le = (Jsonparsercls)parser.readValueAs(Jsonparsercls.class);
wrapperlist.add(new ProWrapper(le.author,le.title,le.description,le.url,le.urlToImage,le.publishedAt));
}
}
}
}
}
}
public class Jsonparsercls{
public String author{get;set;}
public String title{get;set;}
public String description{get;set;}
public String url{get;set;}
public String urlToImage{get;set;}
public String publishedAt{get;set;}
}
public class ProWrapper
{
public String author{get;set;}
public String title{get;set;}
public String description{get;set;}
public String url{get;set;}
public String urlToImage{get;set;}
public String publishedAt{get;set;}
public ProWrapper(String author,String title,String description,String url, String urlToImage ,
String publishedAt)
{
this.author = author;
this.title = title;
this.description = description;
this.url = url;
this.urlToImage = urlToImage;
this.publishedAt = publishedAt;
}
}// wrapper ends
}
I got the output in different way.Thanks...
U can convert the list wrapperlist to string and assign to responsemsg to get your requirment.