图片不会从网址加载

时间:2014-12-27 19:14:12

标签: php android mysql android-imageview

您好我正在构建一个应用程序,它使用来自mysql的数据填充listview。

问题是除了图像之外还显示所有其他数据。我正在使用此方法从url

获取图像
public Bitmap getBitmapFromUrl(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

然后我的适配器像这样使用setImageBitmap到imageview

ImageView stockImage = (ImageView)stockView.findViewById(R.id.imagestartinglist);
            stockImage.setImageBitmap(current.getStockImage());

我从像这样的JSON获取图像

public void ListDrawer() {
        customList = new ArrayList<StockList>();
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("name");
                String price = jsonChildNode.optString("price");
                String price1 = jsonChildNode.optString("price1");
                String price2 = jsonChildNode.optString("price2");
                String price3 = jsonChildNode.optString("price3");
                String price4 = jsonChildNode.optString("price4");
                String price5 = jsonChildNode.optString("price5");
                String price6 = jsonChildNode.optString("price6");
                String price7 = jsonChildNode.optString("price7");
                String price8 = jsonChildNode.optString("price8");
                String price9 = jsonChildNode.optString("price9");
                String price10 = jsonChildNode.optString("price10");
                String price11 = jsonChildNode.optString("price11");
                String price12 = jsonChildNode.optString("price12");
                String price13 = jsonChildNode.optString("price13");
                String price14 = jsonChildNode.optString("price14");
                String price15 = jsonChildNode.optString("price15");

                String image = jsonChildNode.optString("image");

                Bitmap bmp = getBitmapFromUrl(image);

                loipesTimes = new String[]{price1, price2, price3, price4, price5, price6, price7, price8, price9,
                        price10, price11, price12, price13, price14, price15};
                customList.add(new StockList(name, price, bmp, loipesTimes));
            }
        } catch (Exception e) {
            Intent intent1 = new Intent(ListLoaderActivity.this,
                    RefreshActivity.class);
            startActivity(intent1);
            ListLoaderActivity.this.finish();
        }

        ArrayAdapter adapter = new MyStocksAdapter(ListLoaderActivity.this, R.layout.list_item, customList);
        adapter.notifyDataSetChanged();
        startList.setAdapter(adapter);
    }

我从mysql数据库中检索数据的php文件是Here

任何想法?在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

我终于通过使用This Guy

中的这个库来实现这一目标

那样调整我的适配器
ImageView stockImage = (ImageView)stockView.findViewById(R.id.imagestartinglist);
            Ion.with(stockImage).placeholder(R.drawable.ic_launcher_stock_custom_icon).error(R.drawable.ic_launcher_stock_custom_icon).load(current.getStockImage());

我加载数据的方法如下:

public void ListDrawer() {
        customList = new ArrayList<StockList>();
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                name = jsonChildNode.optString("name");
                price = jsonChildNode.optString("price");
                price1 = jsonChildNode.optString("price1");
                price2 = jsonChildNode.optString("price2");
                price3 = jsonChildNode.optString("price3");
                price4 = jsonChildNode.optString("price4");
                price5 = jsonChildNode.optString("price5");
                price6 = jsonChildNode.optString("price6");
                price7 = jsonChildNode.optString("price7");
                price8 = jsonChildNode.optString("price8");
                price9 = jsonChildNode.optString("price9");
                price10 = jsonChildNode.optString("price10");
                price11 = jsonChildNode.optString("price11");
                price12 = jsonChildNode.optString("price12");
                price13 = jsonChildNode.optString("price13");
                price14 = jsonChildNode.optString("price14");
                price15 = jsonChildNode.optString("price15");

                image = jsonChildNode.optString("image");

                loipesTimes = new String[]{price1, price2, price3, price4, price5, price6, price7, price8, price9,
                        price10, price11, price12, price13, price14, price15};
                customList.add(new StockList(name, price, image, loipesTimes));

            }
        } catch (Exception e) {
            Intent intent1 = new Intent(ListLoaderActivity.this,
                    RefreshActivity.class);
            startActivity(intent1);
            ListLoaderActivity.this.finish();
        }

        ArrayAdapter adapter = new MyStocksAdapter(ListLoaderActivity.this, R.layout.list_item, customList);
        adapter.notifyDataSetChanged();
        startList.setAdapter(adapter);
    }

每个项目应如何显示:

public StockList(String stockCurrentName, String stockCurrentPrice, String stockImage, String[] restPrices) {
        this.stockCurrentName = stockCurrentName;
        this.stockCurrentPrice = stockCurrentPrice;
        this.stockImage = stockImage;
        this.restPrices = restPrices;
    }

而不是我在构造函数中使用的位图,我现在正在使用一个字符串,它就像一个魅力!!!