Note that there are some explanatory texts on larger screens.

plurals
  1. POType mismatch: cannot convert from Object to JSONObject
    primarykey
    data
    text
    <p>I'm relatively new to Android development and am writing my first REST-based app. I've opted to use the <a href="http://loopj.com/android-async-http/" rel="nofollow">Android Asynchronous HTTP Client</a> 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:</p> <pre><code> 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); } }); } </code></pre> <p>The <code>String venueName = firstVenue.getString("name");</code> 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 <code>getJSONObject(0)</code> instead of <code>get(0)</code> 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?</p> <p>Thanks so much.</p> <p>EDIT:</p> <p>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: <code>venueName = firstVenue.getString("name");</code> and for venueName here: <code>System.out.println(venueName);</code> Even if I initialize <code>String venueName;</code> directly after <code>JSONObject firstVenue;</code> I still get the same error. Any help in resolving these would be greatly appreciated!</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload