[英]Android Studio | Asynctask | JSON Receiving null object reference error when attempting to parse JSON
As the title suggests, I'm having an issue parsing JSON data from a URL with an asynctask in Android Studio. 顾名思义,我在Android Studio中使用asynctask从URL解析JSON数据时遇到问题。 I keep receiving a Null Object Reference error, however if I use the below code in an Eclipse Enterprise project (without the asynctask code, just a regular application), I'm able to parse the JSON just fine. 我一直收到Null Object Reference错误,但是,如果我在Eclipse Enterprise项目中使用以下代码(没有asynctask代码,只是一个常规应用程序),我就可以解析JSON。 Also, the internet works on my emulator, as I am able to access Google on the web browser. 另外,由于我可以在网络浏览器上访问Google,因此互联网可以在我的模拟器上运行。 Could you guys point me in the right direction on how to overcome this issue? 你们能为我指出如何克服这个问题的正确方向吗? I have tried everything that I could think of, and I'm just confused because the code works in a regular Eclipse Java Application project, yet doesn't work in Android Studio. 我已经尝试了所有我能想到的一切,但我感到困惑,因为该代码可在常规的Eclipse Java Application项目中工作,但在Android Studio中却无法工作。
Asynctask Code: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Asynctask代码:@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_buy_item); 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();
}
}
Note: Commented out the Items Item = mapper.readValue() part because I was receiving a "Redundant variable message", but even when I was using this bit of code, and returning "Items", I still received the Null Object Reference 注意:注释掉Items Item = mapper.readValue()部分是因为我收到了“冗余变量消息”,但是即使当我使用这段代码并返回“ Items”时,我仍然收到了Null Object Reference
JSON: JSON:
{"Items":[{"Email":"jared.hart1@gmail.com","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":"jhart@radixx.com","Description":"Free Macbook
Air, I promise","Phone":"3433215565","Title":"Lenovo
Laptop","Id":"3"}]}
JSON Class Item: 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 Class ItemDetails: 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;}
}
I figured it out!! 我想到了!! In my Manifest file, I had inside the application tag. 在清单文件中,我在application标记内。 I debugged my application, and discovered that I was getting the Null value from the catch block of my code, due to an Internet Security Permission Exception. 我调试了我的应用程序,发现由于Internet安全权限异常,我从代码的catch块中获取了Null值。 After adding the code outside of the application tag, everything worked! 在application标签之外添加代码后,一切正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.