2016-11-25 36 views
0

正如標題所示,我在使用Android Studio中的asynctask從URL解析JSON數據的問題。我不斷收到一個空對象引用錯誤,但是如果我在Eclipse Enterprise項目中使用下面的代碼(沒有asynctask代碼,只是一個普通的應用程序),我能夠解析JSON就好了。另外,因爲我可以通過網絡瀏覽器訪問Google,因此互聯網可以在我的模擬器上運行。你們能否指導我如何解決這個問題?我已經嘗試過所有我能想到的內容,而且我只是感到困惑,因爲代碼在常規Eclipse Java應用程序項目中工作,但在Android Studio中無法工作。Android Studio | Asynctask | JSON在試圖解析JSON時接收空對象引用錯誤

的AsyncTask代碼: @Override 保護無效的onCreate(捆綁savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_buy_item);

 class CallDBQuery extends AsyncTask<URL, Void, Item> { 
      protected Item doInBackground(URL...url) { 
       try { 
        URL urls = new 
       URL("XXX"); 

        ObjectMapper mapper = new ObjectMapper(); 
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 
    false); 

        //Item Items = mapper.readValue(urls, Item.class); 

        return mapper.readValue(urls, Item.class); 
       } 
       catch(Exception e) { 
        e.printStackTrace(); 
        return null; 
       } 
      } 
      protected void onPostExecute(Item Items) { 
       TextView fieldTxt = (TextView) 
       findViewById(R.id.View2); 

     fieldTxt.setText(Items.getItems().get(0).GetTitle().toString()); 
      } 
     } 
     try { 
      URL url = new 
       URL("XXX"); 

      new CallDBQuery().execute(url); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

注:評論出項目項目= mapper.readValue()部分因爲我是 接收「冗餘變量消息」,但即使當我使用的代碼此位,並返回「項目」,我還收到了空對象引用

JSON:

{"Items":[{"Email":"[email protected]","Description":"Surface Pro 4 
for sale, in excellent condition, basically brand 
new!","Phone":"4077603835","Title":"Surface Pro 4","Id":"1"}, 
{"Email":null,"Description":null,"Phone":null,"Title":"Macbook 
Air","Id":"2"},{"Email":"[email protected]","Description":"Free Macbook 
Air, I promise","Phone":"3433215565","Title":"Lenovo 
Laptop","Id":"3"}]} 

JSON類項目:

public class Item { 
    @JsonProperty 
    private List<ItemDetails> Items = new ArrayList<ItemDetails>(); 

    public List<ItemDetails> getItems() {return this.Items;} 
    public void setItems(List<ItemDetails> Items) {this.Items = Items;} 
} 

JSON類ItemDetails:

public class ItemDetails { 
    @JsonProperty 
    private String Id; 

    @JsonProperty 
    private String Title; 

    @JsonProperty 
    private String Phone; 

    @JsonProperty 
    private String Email; 

    @JsonProperty 
    private String Description; 

    public String GetId() {return this.Id;} 
    public void SetId(String Id) {this.Id = Id;} 

    public String GetTitle() {return this.Title;} 
    public void SetTitle(String Title) {this.Title = Title;} 

    public String GetPhone() {return this.Phone;} 
    public void SetPhone(String Phone) {this.Phone = Phone;} 

    public String GetEmail() {return this.Email;} 
    public void SetEmail(String Email) {this.Email = Email;} 

    public String GetDescription() {return this.Description;} 
    public void SetDescription(String Description) {this.Description = 
    Description;} 
} 
+0

作爲一個附註,我不確定這個信息是否有幫助,但我在Macbook Air上編碼,並使用Wifi –

回答

0

我想通了!在我的Manifest文件中,我有應用程序標籤。我調試了我的應用程序,並發現由於Internet安全權限異常,我從代碼的catch塊中獲取了Null值。在應用程序標籤之外添加代碼後,一切正常!

+0