1

我是 android 应用程序开发的新手。有人可以建议我如何在 android 中使用 JSON 解析在 gridview 中显示图像吗?

4

2 回答 2

3

你到底想做什么,因为 json 解析和在 gridview 中显示图像是两件不同的事情。要么你想从服务器获取图像并使用 json 解析来解析 URL。或者让你的问题清楚地被每个人理解。

于 2013-11-15T06:42:31.440 回答
2

在您的项目中添加此类:JsonParser

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        // TODO Auto-generated method stub

        Bitmap bitmap = null;
        try {

            URL aURL = new URL(params[0]);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            // Buffered is always good for a performance plus.
            BufferedInputStream bis = new BufferedInputStream(is);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize =5;
            // Decode url-data to a bitmap.
            bitmap = BitmapFactory.decodeStream(bis, null, options);

            // Close BufferedInputStream
            bis.close();

            // Close InputStream
            is.close();

            return bitmap;
        } catch (IOException e1) {
            e1.printStackTrace();
            return null;
        }

    }

在主要活动中:- 添加网格视图并设置适配器使用您的 URL 调用 asyntask

ImageDownloader d=new ImageDownloder();
Bimap b=d.execute("url").get();
grid = (GridView) findViewById(R.id.grid);// grid view ti view all photos
AdapterForPics adap = new AdapterForPics(Arraylist, Context);
grid.setAdapter(adap);
于 2013-11-15T06:43:50.840 回答