我从json获取缩略图。我已经解析了json并加载了缩略图,但是一旦我打开应用程序,我的应用程序就崩溃了。 LogCat在NullpointerException
中显示AppController.getInstance().getImageLoader()
。我参考了here。
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_sales_return);
private class MyAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private ViewHolder holder = null;
private Model_BarcodeDetail data;
ImageLoader imageLoader = AppController.getInstance().getImageLoader(); // getting NullPointerException here
public MyAdapter() {
inflater = LayoutInflater.from(Activity_Sales_Return.this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return true;
}
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
try {
if (convertView == null) {
holder = new ViewHolder();
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.table_row_sales_return, null);
if (imageLoader == null)
imageLoader =
AppController.getInstance().getImageLoader();
holder.thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
data = (Model_BarcodeDetail) getItem(position);
scannedBarcode = list.get(position).getBarcode();
}
}
}
解析json代码:
public Model_BarcodeDetail GetBarcodeDetail(String api_token, String barcode) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(Utility.BASE_URL
+ "?q=webservice/pdt/barcode_detail&token=" + api_token
+ "&barcode=" + barcode + "&return=json");
String result = "";
Model_BarcodeDetail data = null;
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = httpClient.execute(postRequest, responseHandler);
JSONObject jsonObj = new JSONObject(result);
JSONObject obj1 = jsonObj.getJSONObject("t2tapi");
boolean error = obj1.has("errors");
if (!error) {
data = new Model_BarcodeDetail();
data.setDatetime(obj1.getString("datetime"));
data.setSuccess(obj1.getString("success"));
JSONObject obj2 = obj1.getJSONObject("data");
data.setName(obj2.getString("name"));
data.setThumbnailUrl(obj2.getString("image"));
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
Json Logcat警告:
04-01 11:20:26.834: W/System.err(3844): org.json.JSONException: No value for image
04-01 11:20:26.854: W/System.err(3844): at org.json.JSONObject.get(JSONObject.java:355)
04-01 11:20:26.854: W/System.err(3844): at org.json.JSONObject.getString(JSONObject.java:515)
04-01 11:20:26.854: W/System.err(3844): at com.example.pdt.Activity_Sales_Return.GetBarcodeDetail(Activity_Sales_Return.java:3282)
04-01 11:20:26.864: W/System.err(3844): at com.example.pdt.Activity_Sales_Return$BackgroundTask.doInBackground(Activity_Sales_Return.java:3186)
04-01 11:20:26.864: W/System.err(3844): at com.example.pdt.Activity_Sales_Return$BackgroundTask.doInBackground(Activity_Sales_Return.java:1)
04-01 11:20:26.864: W/System.err(3844): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-01 11:20:26.864: W/System.err(3844): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-01 11:20:26.864: W/System.err(3844): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-01 11:20:26.864: W/System.err(3844): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-01 11:20:26.864: W/System.err(3844): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-01 11:20:26.864: W/System.err(3844): at java.lang.Thread.run(Thread.java:841)
原json:
<?xml version="1.0" encoding="UTF-8"?>
<skudetail>
<datetime>Wed, 30 Mar 2016 07:13:26 +0000</datetime>
<success>1</success>
<data>
<name>AM X COL38/44</name>
<reference>000086</reference>
<description>EB 3 X COL DURA 38/44</description>
<barcode>000086004004</barcode>
<price>24.00</price>
<color>NAVY S</color>
<size>40</size>
<stock>11</stock>
<branch>MDT</branch>
<supplier>KAY</supplier>
<supplier_name>DURABLEPRESS BY KAYMANS</supplier_name>
<location></location>
<basesell></basesell>
<vat>A</vat>
<avg_cost>5.600</avg_cost>
<last_cost>0.000</last_cost>
<next_cost>5.600</next_cost>
<group_code>MEN</group_code>
<alias_code></alias_code>
<type>TRF</type>
<remarks1>x</remarks1>
<remarks2></remarks2>
<listed_days></listed_days>
<title></title>
<type_name>FLAT FRONT TROUSERS</type_name>
<user1_name></user1_name>
<user2_name></user2_name>
<meta_keywords></meta_keywords>
<meta_description></meta_description>
<image>http://staging.gait-
way.com/files/product/32106/1459320344_410cCvu-IsL._UX466_.jpg</image>
</data>
</skudetail>
答案 0 :(得分:1)
我认为你错了,因为你有 XML数据但不是JSON数据,你可以用
验证http://www.w3schools.com/xml/xml_validator.asp
您可以使用XMLDomParser获取Image标记中的值并根据需要使用它,您可以参考下面的链接获取XML解析的参考
http://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm
并且也可以参考这可能有帮助
http://www.tutorialspoint.com/json/json_comparison.htm
并且您可能还想使用Picasso(更轻)或Glide(更多功能),因为它可以减轻您对图像加载到视图中的担忧
希望这有帮助