Error on JSON fetch…

This topic contains 5 replies, has 3 voices, and was last updated by  Kiran 4 years, 7 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #510038

    Rodney Chatman
    Expired Member
    Post count: 89

    Hello,

    I am getting the following error when attempting to fetch and parse json output from the api:

    D/newwwss: https://www.thesablebusinessdirectory.com/wp-json/geodir/v2/business
    D/responsejson: https://www.thesablebusinessdirectory.com/wp-json/geodir/v2/business
    W/System.err: org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONObject
    W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)

    Here is the formatted json output of the first listing via the api:

    
    
    [ 
       { 
          "id":26348,
          "title":{ 
             "raw":"Cigarro\u2019s Cigar Lounge",
             "rendered":"Cigarro\u2019s Cigar Lounge"
          },
          "slug":"cigarros-cigar-lounge",
          "link":"https:\/\/www.thesablebusinessdirectory.com\/business\/united-states\/illinois\/harvey\/cigar-lounge\/cigarros-cigar-lounge\/",
          "status":"publish",
          "type":"gd_business",
          "author":2,
          "date":"2019-07-10T14:08:25",
          "date_gmt":"2019-07-10T19:08:25",
          "modified":"2019-07-28T16:10:53",
          "modified_gmt":"2019-07-28T21:10:53",
          "content":{ 
             "raw":"Cigar and Hookah lounge with a wide selection of cigars and hookah flavors. Our intimate setting and stylish furnishings offers a top quality cigar and hookah experience.",
             "rendered":"<p>Cigar and Hookah lounge with a wide selection of cigars and hookah flavors. Our intimate setting and stylish furnishings offers a top quality cigar and hookah experience.<\/p>\n",
             "protected":false
          },
          "default_category":"156",
          "post_category":[ 
             { 
                "id":156,
                "name":"Cigar Lounge",
                "slug":"cigar-lounge"
             },
             { 
                "id":117,
                "name":"Feature",
                "slug":"feature-2"
             }
          ],
          "post_tags":[ 
             { 
                "id":1163,
                "name":"cigar",
                "slug":"cigar"
             },
             { 
                "id":1164,
                "name":"hookah",
                "slug":"hookah"
             }
          ],
          "package_id":"6",
          "street":"16431 Halsted St",
          "country":"United States",
          "region":"Illinois",
          "city":"Harvey",
          "zip":"60426",
          "latitude":"41.5907235",
          "longitude":"-87.63600980000001",
          "mapview":null,
          "mapzoom":"",
          "logo":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/42789201_2185702758333317_4620792543095816192_n-1.jpg|719||",
          "phone":" (708) 295-3912",
          "email":"",
          "website":"",
          "twitter":"",
          "facebook":"https:\/\/www.facebook.com\/cigarroscigarlounge\/",
          "video":"",
          "special_offers":"",
          "business_hours":{ 
             "raw":"[\"Mo 12:00-00:00\",\"Tu 12:00-00:00\",\"We 12:00-00:00\",\"Th 12:00-00:00\",\"Fr 12:00-00:00\",\"Sa 12:00-00:00\",\"Su 12:00-00:00\"],[\"UTC\":\"-6\"]",
             "rendered":{ 
                "days":{ 
                   "Mo":{ 
                      "today":0,
                      "closed":0,
                      "open":0,
                      "day":"Monday",
                      "day_short":"Mon",
                      "day_no":1,
                      "slots":[ 
                         { 
                            "slot":"12:00-00:00",
                            "range":"12:00 pm - 12:00 am",
                            "open":0,
                            "time":[ 
                               "1200",
                               "0000"
                            ],
                            "minutes":[ 
                               720,
                               0
                            ]
                         }
                      ]
                   },
                   "Tu":{ 
                      "today":1,
                      "closed":0,
                      "open":0,
                      "day":"Tuesday",
                      "day_short":"Tue",
                      "day_no":2,
                      "slots":[ 
                         { 
                            "slot":"12:00-00:00",
                            "range":"12:00 pm - 12:00 am",
                            "open":0,
                            "time":[ 
                               "1200",
                               "0000"
                            ],
                            "minutes":[ 
                               2160,
                               1440
                            ]
                         }
                      ]
                   },
                   "We":{ 
                      "today":0,
                      "closed":0,
                      "open":0,
                      "day":"Wednesday",
                      "day_short":"Wed",
                      "day_no":3,
                      "slots":[ 
                         { 
                            "slot":"12:00-00:00",
                            "range":"12:00 pm - 12:00 am",
                            "open":0,
                            "time":[ 
                               "1200",
                               "0000"
                            ],
                            "minutes":[ 
                               3600,
                               2880
                            ]
                         }
                      ]
                   },
                   "Th":{ 
                      "today":0,
                      "closed":0,
                      "open":0,
                      "day":"Thursday",
                      "day_short":"Thu",
                      "day_no":4,
                      "slots":[ 
                         { 
                            "slot":"12:00-00:00",
                            "range":"12:00 pm - 12:00 am",
                            "open":0,
                            "time":[ 
                               "1200",
                               "0000"
                            ],
                            "minutes":[ 
                               5040,
                               4320
                            ]
                         }
                      ]
                   },
                   "Fr":{ 
                      "today":0,
                      "closed":0,
                      "open":0,
                      "day":"Friday",
                      "day_short":"Fri",
                      "day_no":5,
                      "slots":[ 
                         { 
                            "slot":"12:00-00:00",
                            "range":"12:00 pm - 12:00 am",
                            "open":0,
                            "time":[ 
                               "1200",
                               "0000"
                            ],
                            "minutes":[ 
                               6480,
                               5760
                            ]
                         }
                      ]
                   },
                   "Sa":{ 
                      "today":0,
                      "closed":0,
                      "open":0,
                      "day":"Saturday",
                      "day_short":"Sat",
                      "day_no":6,
                      "slots":[ 
                         { 
                            "slot":"12:00-00:00",
                            "range":"12:00 pm - 12:00 am",
                            "open":0,
                            "time":[ 
                               "1200",
                               "0000"
                            ],
                            "minutes":[ 
                               7920,
                               7200
                            ]
                         }
                      ]
                   },
                   "Su":{ 
                      "today":0,
                      "closed":0,
                      "open":0,
                      "day":"Sunday",
                      "day_short":"Sun",
                      "day_no":7,
                      "slots":[ 
                         { 
                            "slot":"12:00-00:00",
                            "range":"12:00 pm - 12:00 am",
                            "open":0,
                            "time":[ 
                               "1200",
                               "0000"
                            ],
                            "minutes":[ 
                               9360,
                               8640
                            ]
                         }
                      ]
                   }
                },
                "extra":{ 
                   "has_open":0,
                   "has_closed":0,
                   "today_range":"12:00 pm - 12:00 am",
                   "current_label":"Closed now",
                   "open_now_label":"Open now",
                   "closed_now_label":"Closed now",
                   "date":"2019-09-24",
                   "time":"12:45:51",
                   "full_date":"2019-09-24 12:45:51",
                   "date_format":"September 24, 2019",
                   "time_format":"12:45 pm",
                   "full_date_format":"September 24, 2019 12:45 pm",
                   "offset":"-6"
                }
             }
          },
          "claimed":{ 
             "raw":"0",
             "rendered":"No"
          },
          "featured":true,
          "rating":0,
          "rating_count":0,
          "featured_media":26350,
          "featured_image":{ 
             "id":"712",
             "title":"",
             "src":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/57360372_2306321916271400_3173321751832559616_n.jpg",
             "thumbnail":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/57360372_2306321916271400_3173321751832559616_n-150x150.jpg",
             "width":720,
             "height":960
          },
          "images":[ 
             { 
                "id":"712",
                "title":"",
                "src":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/57360372_2306321916271400_3173321751832559616_n.jpg",
                "thumbnail":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/57360372_2306321916271400_3173321751832559616_n-150x150.jpg",
                "featured":true,
                "position":"0"
             },
             { 
                "id":"713",
                "title":"",
                "src":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/53588141_2284667001770225_3389107072061669376_o.jpg",
                "thumbnail":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/53588141_2284667001770225_3389107072061669376_o-150x150.jpg",
                "featured":false,
                "position":"1"
             },
             { 
                "id":"715",
                "title":"",
                "src":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/57289400_2307432822826976_987557034553507840_o.jpg",
                "thumbnail":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/57289400_2307432822826976_987557034553507840_o-150x150.jpg",
                "featured":false,
                "position":"2"
             },
             { 
                "id":"716",
                "title":"",
                "src":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/58380872_2307617746141817_5840534096626843648_n.jpg",
                "thumbnail":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/58380872_2307617746141817_5840534096626843648_n-150x150.jpg",
                "featured":false,
                "position":"3"
             },
             { 
                "id":"717",
                "title":"",
                "src":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/53356781_2277938982443027_1449656276781891584_o.jpg",
                "thumbnail":"https:\/\/www.thesablebusinessdirectory.com\/wp-content\/uploads\/2019\/07\/53356781_2277938982443027_1449656276781891584_o-150x150.jpg",
                "featured":false,
                "position":"4"
             }
          ],
          "comment_status":"open",
          "ping_status":"closed",
          "meta":[ 
    
          ],
          "guid":{ 
             "rendered":"https:\/\/www.thesablebusinessdirectory.com\/?post_type=gd_business&p=26348"
          },
          "_links":{ 
             "self":[ 
                { 
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/geodir\/v2\/business\/26348"
                }
             ],
             "collection":[ 
                { 
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/geodir\/v2\/business"
                }
             ],
             "about":[ 
                { 
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/geodir\/v2\/types\/gd_business"
                }
             ],
             "author":[ 
                { 
                   "embeddable":true,
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/wp\/v2\/users\/2"
                }
             ],
             "replies":[ 
                { 
                   "embeddable":true,
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/geodir\/v2\/reviews?post=26348"
                }
             ],
             "version-history":[ 
                { 
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/geodir\/v2\/business\/26348\/revisions"
                }
             ],
             "wp:featuredmedia":[ 
                { 
                   "embeddable":true,
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/wp\/v2\/media\/26350"
                }
             ],
             "wp:attachment":[ 
                { 
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/wp\/v2\/media?parent=26348"
                }
             ],
             "wp:term":[ 
                { 
                   "taxonomy":"gd_business_tags",
                   "embeddable":true,
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/geodir\/v2\/business\/tags?post=26348"
                },
                { 
                   "taxonomy":"gd_businesscategory",
                   "embeddable":true,
                   "href":"https:\/\/www.thesablebusinessdirectory.com\/wp-json\/geodir\/v2\/business\/categories?post=26348"
                }
             ],
             "curies":[ 
                { 
                   "name":"wp",
                   "href":"https:\/\/api.w.org\/{rel}",
                   "templated":true
                }
             ]
          }
       }
    ]

    Here is the code I’m using:

    
    
        @SuppressLint("StaticFieldLeak")
        private void fetchJSON(){
    
            showSimpleProgressDialog(this, "Searching...","Finding Locations Near You",false);
    
            new AsyncTask<Void, Void, String>(){
                protected String doInBackground(Void[] params) {
                    String response="";
                    HashMap<String, String> map=new HashMap<>();
                    try {
                        HttpRequest req = new HttpRequest(jsonURL);
                        response = req.prepare(HttpRequest.Method.POST).withData(map).sendAndReadString();
                    } catch (Exception e) {
                        response=e.getMessage();
                    }
                    return response;
                }
                protected void onPostExecute(String result) {
                    //do something with response
                    Log.d("newwwss",result);
                    onTaskCompleted(result,jsoncode);
                }
            }.execute();
        }
    
        public void onTaskCompleted(String response, int serviceCode) {
            Log.d("responsejson", response.toString());
            switch (serviceCode) {
                case jsoncode:
    
                    if (isSuccess(response)) {
                        removeSimpleProgressDialog();  //will remove progress dialog
                        nearMeModelArrayList = getInfo(response);
                        nearMeAdapter = new NearMeAdapter(this, nearMeModelArrayList);
                        listView.setAdapter(nearMeAdapter);
    
                    }else {
                        Toast.makeText(MainActivity.this, getErrorCode(response), Toast.LENGTH_SHORT).show();
                    }
            }
        }
    
        public ArrayList<NearMeModel> getInfo(String response) {
            ArrayList<NearMeModel> nearMeModelArrayList = new ArrayList<>();
            try {
                JSONObject jsonObject = new JSONObject(response);
                if (jsonObject.getString("id").equals("26348")) {
    
                    JSONArray dataArray = jsonObject.getJSONArray("data");
    
                    for (int i = 0; i < dataArray.length(); i++) {
    
                        NearMeModel playersModel = new NearMeModel();
                        JSONObject dataobj = dataArray.getJSONObject(i);
                        playersModel.setName(dataobj.getString("name"));
                        playersModel.setCountry(dataobj.getString("country"));
                        playersModel.setCity(dataobj.getString("city"));
                        playersModel.setImgURL(dataobj.getString("featured_image"));
                        playersModel.setDescription(dataobj.getString("description"));
                        nearMeModelArrayList.add(playersModel);
    
                    }
                }
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return nearMeModelArrayList;
        }
    
        public boolean isSuccess(String response) {
    
            try {
                JSONObject jsonObject = new JSONObject(response);
                if (jsonObject.optString("id").equals("26348")) {
                    return true;
                } else {
    
                    return false;
                }
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }
    
        public String getErrorCode(String response) {
    
            try {
                JSONObject jsonObject = new JSONObject(response);
                return jsonObject.getString("message");
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return "No data";
        }

    Any assistance would be greatly appreciated.

    #510052

    Alex Rollin
    Moderator
    Post count: 27815

    Thanks for posting, the developers will take a look.

    #510274

    Rodney Chatman
    Expired Member
    Post count: 89

    Hey guys,

    Any update?

    #510304

    Kiran
    Moderator
    Post count: 7069
    #510383

    Rodney Chatman
    Expired Member
    Post count: 89

    Hello,

    The issue I’m having seems to be with verifying the json data. I don’t have the issue when getting a json dump of this type:

    {"status":"true","message":"Data fetched successfully!","data":[{"id":"1","name":"Roger Federer","country":"Switzerland","city":"Basel","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/roger.jpg"},{"id":"2","name":"Rafael Nadal","country":"Spain","city":"Madrid","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/nadal.jpg"},{"id":"3","name":"Novak Djokovic","country":"Serbia","city":"Monaco","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/djoko.jpg"},{"id":"4","name":"Andy Murray","country":"United Kingdom","city":"London","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/murray.jpg"},{"id":"5","name":"Maria Sharapova","country":"Russia","city":"Moscow","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/shara.jpg"},{"id":"6","name":"Caroline Wozniacki","country":"Denmark","city":"Odense","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/woz.jpg"},{"id":"7","name":"Eugenie Bouchard","country":"Canada","city":" Montreal","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/bou.png"},{"id":"8","name":"Ana Ivanovic","country":"Serbia","city":"Belgrade","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/iva.jpg"}]}

    Keep in mind that I’m very much a n00b. I’m not sure how to actually parse data specific to the geodirectory api. It would appear that there is no universal way to interact with any api, I hope I’m wrong about that. Specific examples of how to interact with the gd api would be most helpful.

    #510531

    Kiran
    Moderator
    Post count: 7069

    Hi Rodney,

    We just pass data to json just like WordPress default posts do. Do you see similar issue with WordPress posts API. Please try that and let us know how it goes.

    Kiran

Viewing 6 posts - 1 through 6 (of 6 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket