user2163853
user2163853

Reputation: 917

Type mismatch: cannot convert from Object to JSONObject

I'm relatively new to Android development and am writing my first REST-based app. I've opted to use the Android Asynchronous HTTP Client to make things a bit easier. I'm currently just running through the main "Recommended Usage" section on that link, essentially just creating a basic static HTTP client. I'm following the code given, but changing it around to refer to a different API. Here's the code in question:

    public void getFactualResults() throws JSONException {
    FactualRestClient.get("q=Coffee,Los Angeles", null, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(JSONArray venues) {
            // Pull out the first restaurant from the returned search results
            JSONObject firstVenue = venues.get(0);
            String venueName = firstVenue.getString("name");

            // Do something with the response
            System.out.println(venueName);

        }
    });
}

The String venueName = firstVenue.getString("name"); line is currently throwing an error in Eclipse: "Type mismatch: cannot convert from Object to JSONObject". Why is this error occurring? I searched other threads which led me to try using getJSONObject(0) instead of get(0) but that led to further errors and Eclipse suggesting using try/catch. I haven't changed any of the code on the tutorial, save for the variable names and URL. Any thoughts/tips/advice?

Thanks so much.

EDIT:

Here is the onSuccess method, modified to include the try/catch blocks suggested. Eclipse now shows the "local variable may not have been initialized" for firstVenue here: venueName = firstVenue.getString("name"); and for venueName here: System.out.println(venueName); Even if I initialize String venueName; directly after JSONObject firstVenue; I still get the same error. Any help in resolving these would be greatly appreciated!

public void onSuccess(JSONArray venues) {
            // Pull out the first restaurant from the returned search results
            JSONObject firstVenue;
            try {
                firstVenue = venues.getJSONObject(0);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String venueName;
            try {
                venueName = firstVenue.getString("name");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Do something with the response
            System.out.println(venueName);

        }

Upvotes: 0

Views: 3875

Answers (4)

ashack
ashack

Reputation: 1202

The solution provided by Shail Adi only worked for me by setting the initial values of firstVenue and venueName to null. Here's my code:

        JSONObject firstVenue = null;
        try {
            firstVenue = (JSONObject)venues.get(0);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String venueName = null;
        try {
            venueName = firstVenue.getString("name");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Do something with the response
        System.out.println(venueName);

Upvotes: 0

bala
bala

Reputation: 11

convert obj to json Object:

Object obj = JSONValue.parse(inputParam); JSONObject jsonObject = (JSONObject) obj;

Upvotes: 0

user2138579
user2138579

Reputation:

You can try to convert object you are getting from querying to String and then use

final JSONObject jsonObject = new JSONObject(stringresult);

I was getting same error earlier, it worked for me.

Upvotes: 1

Jeremy Roman
Jeremy Roman

Reputation: 16345

Yes, you should be using getJSONObject to ensure that the value you obtain is a JSON object. And yes, you should catch the possible JSONException which is thrown if that index in the array doesn't exist, or does not contain an object.

It'll look something like this:

JSONObject firstVenue;
try {
    firstVenue = venues.get(0);
} catch (JSONException e) {
    // error handling
}

Upvotes: 0

Related Questions