1

How to solve this error and why this error is occuring

Here is my controller class

 public List<SelectOption> getItems()
    {
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setEndpoint('http://clozer.3spire.net/public/goclozer/country');
        req.setMethod('GET');

        req.setCompressed(false);
        req.setBody('key1=value1&key2=value2');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');  

//        String data = '{ "abcd":"xyz","efgh":"djfh" }';
        getAllCountry = (Map<String, String>)JSON.deserialize(res.getBody(),Map<String, String>.class);

        List<SelectOption> option = new List<SelectOption>();

         option.add(new SelectOption('0','--None--'));        
        for(String c : getAllCountry.values())
        {
            option.add(new SelectOption(c,c));
        }

        return option;
    }

This is the json result

{"0":"Aruba","1":"Antigua and Barbuda","2":"United Arab Emirates","3":"Afghanistan","4":"Algeria","5":"Azerbaijan","6":"Albania","7":"Armenia","8":"Andorra","9":"Angola"}
1

Try the url in a browser. It's not working anymore.

This url http://clozer.3spire.net/public/goclozer/country results in this page:

Not Found
The requested URL /public/goclozer/country was not found on this server.

And this HTML:

<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
    <head>
        <title>404 Not Found</title>
    </head>
    <body>
        <h1>Not Found</h1>
        <p>The requested URL /public/goclozer/country was not found on this server.</p>

    </body>
</html>

Which fully explains this error System.JSONException: Unexpected character ('<'

Because you are expecting JSON and you are receiving a 404 page. You will need to find another country list.

| improve this answer | |
0

Sorry, is it all code? Where is line that perform request?

...
HttpResponse response = http.send(req);
...

upd. in your code should be this lines:

...
req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
res = http.send(req);
getAllCountry = (Map<String, String>)JSON.deserialize(res.getBody(),Map<String, String>.class); 
...
| improve this answer | |
  • yes thats all.and the things is that i use the code before and it worked fine – moin khan Jan 26 '16 at 15:03
  • @moinkhan it's impossible, because now you have empty body in response. May be you delete this line? – RasMisha Jan 26 '16 at 15:04
  • I just want to get json data from a remote site and then bind them into selectOption.is there any way to do that. – moin khan Jan 26 '16 at 15:18
  • I understand, but in your code there is no calls to remote site. If you add line res = http.send(req); it must be ok – RasMisha Jan 26 '16 at 15:23
  • now i understand where the problem is.I missed try that line – moin khan Jan 26 '16 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.