2013-03-13 59 views
0

我對Android開發比較陌生,正在編寫我的第一個基於REST的應用程序。我選擇使用Android Asynchronous HTTP Client使事情變得更簡單。我目前只是通過該鏈接的主要「推薦用法」部分,本質上只是創建一個基本的靜態HTTP客戶端。我遵循給定的代碼,但改變它來引用不同的API。這裏是有問題的代碼:類型不匹配:無法從Object轉換爲JSONObject

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); 

     } 
    }); 
} 

String venueName = firstVenue.getString("name");線是目前在Eclipse中拋出一個錯誤:「類型不匹配:不能從對象轉換爲JSONObject的」。爲什麼會發生此錯誤?我搜索了其他線程,導致我嘗試使用getJSONObject(0)而不是get(0),但這導致了進一步的錯誤,Eclipse提示使用try/catch。我沒有改變教程中的任何代碼,除了變量名和URL。任何想法/提示/建議?

非常感謝。

編輯:

這裏是的onSuccess方法,修改爲包括try/catch塊建議。 Eclipse現在顯示firstVenue的「局部變量可能未初始化」:venueName = firstVenue.getString("name");和場所名稱:System.out.println(venueName);即使在JSONObject firstVenue;之後直接初始化String venueName;,我仍然得到相同的錯誤。任何幫助解決這些將不勝感激!

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); 

     } 
+0

請問您可以粘貼您通過獲取請求檢索的場地價值嗎? – 2013-03-13 05:48:31

+0

使用GSON從谷歌,因爲它很容易,快速,重量輕,並採取了所有的痛苦,這離開https://code.google.com/p/google-gson/ – 2013-06-17 22:05:36

回答

0

是的,你應該用getJSONObject,以確保您獲得的值是一個JSON對象。是的,你應該抓住可能的JSONException如果該數組中的索引不存在或者不包含對象,則拋出該數據。

它會是這個樣子:

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

謝謝。那麼第1行的錯誤處理沒有幫助呢?可以刪除嗎? ''FactualRestClient.get()'被定義爲:public static void get(String url,RequestParams params,AsyncHttpResponseHandler responseHandler){client.get(getAbsoluteUrl(url),params,responseHandler); \t}'因此它已經內置錯誤處理...你能澄清嗎? – user2163853 2013-03-13 05:38:57

+0

您現有的'throws'子句捕獲'getFactualResults'方法中拋出的'JSONException's,但在響應處理程序的'onSuccess'方法中會發生異常。所以這種方法必須拋出異常(這將需要修改接口和'FactualRestClient')。即便如此,因爲這是異步的,所以你不能在getFactualResults的運行時間內拋出它。最好在'onSuccess'中捕獲它,或者如果它真的出乎意料,就拋出一個'RuntimeException'(這是未選中的)。 – 2013-03-13 05:59:19

+0

OK,所以我這樣做了,但後來遇到了在try/catch塊內初始化firstVenue和venueName的問題 - 錯誤是「局部變量fi​​rstVenue可能未被初始化」。即使我在try/catch之前初始化,它仍然會發生。思考? – user2163853 2013-03-14 03:38:09

1

你可以嘗試轉換對象,你都可以從查詢到字符串,然後使用

最終的JSONObject的JSONObject =新的JSONObject(stringresult);

我以前得到同樣的錯誤,它爲我工作。

+0

對不起,你能澄清一下如何適合現有的代碼嗎?謝謝 – user2163853 2013-03-13 05:43:16

+0

你得到的場地的價值是什麼? – 2013-03-13 07:19:14

+0

我不確定你的意思 - 場地在JSONArray中返回並且是JSONObjects。 – user2163853 2013-03-13 13:34:56

0

轉換OBJ到JSON對象:

對象OBJ = JSONValue.parse(inputParam); JSONObject jsonObject =(JSONObject)obj;

0

由Shail Adi提供的解決方案只能通過將firstVenue和venueName的初始值設置爲null來爲我工作。以下是我的代碼:

 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); 
相關問題