You need to sign in to do that
Don't have an account?
Mayank Raj 22
Can someone tell me what is the issue with this code. I don't seem to get the value displayed in the VF Page.
Controller:
/* This class is used to display a list of contacts from a rest api (https://api.androidhive.info/contacts/) to a VF Page and also has a checkbox to save the desired Contact wihtin the Org*/
public class restIntegrationPageController {
List<Contact> conList{get;set;}
public class wrapperforJSON{
public string id{get;set;}
public string name{get;set;}
public String email{get;set;}
public string address{get;set;}
public string gender{get;set;}
public string mobile{get;set;}
public boolean isChecked{get;set;}
}
public List<wrapperforJSON> rList{get;set;}
public wrapperforJSON singleRec{get;set;}
public void Method1(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://api.androidhive.info/contacts/');
request.setMethod('GET');
HttpResponse response = http.send(request);
List<String> idList = new List<String>();
List<String> nameList = new List<String>();
List<String> emailList = new List<String>();
List<String> addressList = new List<String>();
List<String> genderList = new List<String>();
List<String> mobileList = new List<String>();
JSONParser parser = JSON.createParser(response.getBody());
while(parser.nextToken() != null){
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'id'){
parser.nextToken();
idList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONTOKEN.FIELD_NAME && parser.getText() == 'Name'){
parser.nextToken();
nameList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Email'){
parser.nextToken();
emailList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Address'){
parser.nextToken();
addressList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FIELD_Name && parser.getText() == 'Gender'){
parser.nextToken();
genderList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FielD_Name && parser.getText() == 'Mobile'){
parser.nextToken();
mobileList.add(parser.getText());
}
}
rList = new List<wrapperforJSON>();
for(Integer i=0;i<idList.size();i++){
singleRec = new wrapperforJSON();
singleRec.id = idList[i];
singleRec.name = nameList[i];
singleRec.email = emailList[i];
singleRec.Address = addressList[i];
singleRec.Gender = genderList[i];
singleRec.Mobile = mobileList[i];
singleRec.isChecked = False;
rList.add(singleRec);
}
}
}
VF:
<apex:page controller="restIntegrationPageController">
<apex:form >
<apex:pageBlock title="Rest Integration Example 1">
<apex:pageBlockTable value="{!rList}" var="r">
<apex:column value="{!r.Id}" headerValue="Id"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
/* This class is used to display a list of contacts from a rest api (https://api.androidhive.info/contacts/) to a VF Page and also has a checkbox to save the desired Contact wihtin the Org*/
public class restIntegrationPageController {
List<Contact> conList{get;set;}
public class wrapperforJSON{
public string id{get;set;}
public string name{get;set;}
public String email{get;set;}
public string address{get;set;}
public string gender{get;set;}
public string mobile{get;set;}
public boolean isChecked{get;set;}
}
public List<wrapperforJSON> rList{get;set;}
public wrapperforJSON singleRec{get;set;}
public void Method1(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://api.androidhive.info/contacts/');
request.setMethod('GET');
HttpResponse response = http.send(request);
List<String> idList = new List<String>();
List<String> nameList = new List<String>();
List<String> emailList = new List<String>();
List<String> addressList = new List<String>();
List<String> genderList = new List<String>();
List<String> mobileList = new List<String>();
JSONParser parser = JSON.createParser(response.getBody());
while(parser.nextToken() != null){
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'id'){
parser.nextToken();
idList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONTOKEN.FIELD_NAME && parser.getText() == 'Name'){
parser.nextToken();
nameList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Email'){
parser.nextToken();
emailList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Address'){
parser.nextToken();
addressList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FIELD_Name && parser.getText() == 'Gender'){
parser.nextToken();
genderList.add(parser.getText());
}
if(parser.getCurrentToken() == JSONToken.FielD_Name && parser.getText() == 'Mobile'){
parser.nextToken();
mobileList.add(parser.getText());
}
}
rList = new List<wrapperforJSON>();
for(Integer i=0;i<idList.size();i++){
singleRec = new wrapperforJSON();
singleRec.id = idList[i];
singleRec.name = nameList[i];
singleRec.email = emailList[i];
singleRec.Address = addressList[i];
singleRec.Gender = genderList[i];
singleRec.Mobile = mobileList[i];
singleRec.isChecked = False;
rList.add(singleRec);
}
}
}
VF:
<apex:page controller="restIntegrationPageController">
<apex:form >
<apex:pageBlock title="Rest Integration Example 1">
<apex:pageBlockTable value="{!rList}" var="r">
<apex:column value="{!r.Id}" headerValue="Id"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
I have created a constructor and called method1 inside it.
Do not forget to add a remote site setting for the url
All Answers
I have created a constructor and called method1 inside it.
Do not forget to add a remote site setting for the url