• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Not able to return a value from a method because of its try catch block

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone! I have encountered a problem that I cannot solve for a couple of days. The thing is I have created a parser class that has a method called parseJSON. The method returns a String value that has the list of place_ids from google places API. When I debug the code I can see that the method works fine and the return value contains the information I need. However, when I try to call this method outside of the parser class it always returns the value of the string that was initialised first, whether is null or any other value. The main functionality of the code is inside a try-catch block because the new JSONObject throws JSONException error. I tried many ways to solve however nothing changed yet. Hope that you can see something I can't.

Parser Class.



MapsActivity.Class



Basically the code works inside the method but outside when called returns nothing. I have googled and it seems there is a problem with the try-catch block. JSON exception is thrown but without catching it the code won't compile.

This is where my parser is called as well. Maybe there is a connection.



 
Bartender
Posts: 209
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the error say?
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONObject

But the debugging does not show any errors. Why is that. And the code is simmilar to the other method I got, nonetheless that one works just fine. This one is getting the places on the map.

 
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marin Capranov wrote: I have encountered a problem that I cannot solve for a couple of days. The thing is I have created a parser class that has a method called parseJSON


Probably it throws an exception this method hence it returns a value assigned during variable's declaration.

But forget that for the moment - main problem in my opinion is, that this method does more than it should and so becomes more difficult to debug. Name also not descriptive.

Decompose this method to few smaller methods and you possibly at the same time will narrow down where the problem is.

The proposed methods may look like:

1.


2.


And what exception message gives you as an error?
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the error message is org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONObject

Honestly, I can't see why?
 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marin Capranov wrote:the error message is org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONObject

Honestly, I can't see why?



You have such line of code:

Does url is a well formed json object?
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what my class for downloading urls does. I have tried to change it to JSONObject but unlucky so far.



And regarding that line of code I will show what I have got:



and that's when I call that method



I hope it will clarify a bit
 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marin Capranov wrote:I hope it will clarify a bit


It adds to complexity and reveals that you have a bit of a mess.

https://coderanch.com/t/726814/java/return-method-catch-block#3379733
In this post of yours (link right above), MapsActivity.java, Line: 3 you have line of code:


By looking to parseJSON() method implementation of yours:

And in one of your post you write:

Marin Capranov wrote:The main functionality of the code is inside a try-catch block because the new JSONObject throws JSONException error. I tried many ways to solve however nothing changed yet. Hope that you can see something I can't.
...
org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONObject


So the problem is clear, that you are passing an String url and trying to treat it as string representation of json object. So that is a problem.

Example:

You pass: "https://coderanch.com"
Expected: {"name":"John", "age":30}

And a bit unclear after you posted bunch of unrelated code whether we are talking about the same problem you initialy raised or something else?
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You pass: "https://coderanch.com"
Expected: {"name":"John", "age":30}

I believe that's the problem, too. But what you suggest to do? I can't see any other ways of parsing that URL. After all, I got that from the official Google website. I don't want to pass an entire JSON file because it is huge. That's why I got that URL.

And regarding the bunch of code:

This one here is just a demonstration that the parser actually works and it is pretty similar to parseJSON where JSON is as a string.



 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marin Capranov wrote:I believe that's the problem, too. But what you suggest to do? I can't see any other ways of parsing that URL. After all, I got that from the official Google website. I don't want to pass an entire JSON file because it is huge. That's why I got that URL.


Well, if the code construct requires a string json representation to be passed in order successfully to be parsed to JsonObject - that is exactly what you need to do -> to pass string json representation. No other options.

To me what it sounds like what you may want to do, is having some sort of URL which submitting you get back json object, if that's the case, this is what you need to pass, the returned string json. Once you have JsonObject, then you move on further - get needed attribute(s), which is perhaps what you refering to as "place_id".

I'm a bit lost to be honest with all that amount of code on which part you are still confused.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you mention I need to return an object out of the URL. I don't know if you ever heard of the Places API but they provide this URL that you can use in order to get full JSON file. Therefore, when putting in the browser search bar the: https://maps.googleapis.com/maps/api/place/textsearch/json?query=ATM&location=52.448044,-1.8870055&radius=1000&opennow&minprice=null&maxprice=null&key=<YOUR_API_KEY>;
you get a massive JSON file.

And that's how I build up the URL link that I pass into JSONObject parameter.

 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so you build an URL, which consists of an API address + query parameters you pass on in order to filter resulting response/json object. That’s as a concept is correct.

Now, as a response of submitting request to that URL, you should get back json object, which you are going to pass (json response and not url) on to your parseJson() method.

How ado you submit a request to that URL? Please show us the code which handles that.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's is the code that handles urls.




Also, you can have a look at this class as well as it is involved in the process.



 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you need to pass the data returned by that method (that is, the response from the Places API which is JSON) into your json parsing method, not the URL.
 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marin Capranov wrote:... Places API but they provide this URL that you can use in order to get full JSON... you get a massive JSON file.


You mentioned couple of times that it returns a massive JSON file. Do you see it as a problem in any way? Other than that, as been mentioned few times already, you need to pass that "massive" string representation of json object to your json parser.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, ok! So if I understand right? all I need is the [{object: bla, bla bla}] and not the https://maps.googleapis.com/maps/api/directions/json?origin=52.473772,-1.898288&destination=52.4744417,-1.8966049&alternatives=true&mode=DRIVING...?

But if is that how do I extract that json object out of the https link?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your readUrl method above that's exactly what you're doing (at least that's what it looks like to me).
That is what should be in the data String that is returned.
 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marin Capranov wrote:But if is that how do I extract that json object out of the https link?


You don't extract it from URL. URL is an address of a resource (representation of something in a json format in your case), hence the name URL - universal resource locator. It knows how to find the resource on the Internet given the path it is located.

In dumb analogy, think of it, as calling method getName() which returns you a name. So you "call" url which returns you a json object.
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a good way of parsing urls?



Also, I have a theory that this does not work because it is not the first task to be completed.



And that's the code where I have the method



And finally but most importantly, this is where the called method returns not what I expect it to do

 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically that's my entire code more or less. Other bits are not involved in this problem.
 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see you addressed any problems we discussed in this thread recently.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Again, you are passing in the url to your parser.
Your parser is expecting the response from the url, not the url itself.

At the moment I see no connection between your code that gets the json data (the doInBackground that populates the googleDirectionsData) and the parser code.
 
Beauty is in the eye of the tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic