Android studio将图像url加载到listview中

Android studio将图像url加载到listview中,android,android-studio,asynchronous,bitmap,phpmyadmin,Android,Android Studio,Asynchronous,Bitmap,Phpmyadmin,我想用文本和图像填充列表视图。 我通过mysql数据库以JSON格式接收这些信息。 我有一个名为“FOTO”的字段,我将照片的路径存储在其中,如:“” 我用这段代码得到android.os.NetworkOnMainThreadException,但我不知道如何做不同 我解析JSON并将值传递给listadapter。我还需要传递图标,以便显示位图值,但我需要从服务器下载它 public class DisplayListView extends AppCompatActivity {

我想用文本和图像填充列表视图。 我通过mysql数据库以JSON格式接收这些信息。 我有一个名为“FOTO”的字段,我将照片的路径存储在其中,如:“”

我用这段代码得到android.os.NetworkOnMainThreadException,但我不知道如何做不同

我解析JSON并将值传递给listadapter。我还需要传递图标,以便显示位图值,但我需要从服务器下载它

public class DisplayListView extends AppCompatActivity {
    final static String TAG = "sb.dl";
    String json_string;
    JSONObject jsonObject;
    JSONArray jsonArray;
    TeamAdapter teamAdapter;
    ListView listView;
    Bitmap icon = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_listview_layout);

        teamAdapter = new TeamAdapter(this, R.layout.row_layout);
        listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(teamAdapter);

        json_string = getIntent().getExtras().getString("json_data");
        Log.d(TAG, "json_string " + json_string);

        try {
            jsonObject = new JSONObject(json_string);
            jsonArray = jsonObject.getJSONArray("risposta");
            int count = 0;

            String nome, num, data;

            while (count < jsonArray.length()) {
                JSONObject JO = jsonArray.getJSONObject(count);
                nome = JO.getString("NOME");
                num = JO.getString("NUMERO");
                data = JO.getString("DATA_NASCITA");
                String url = JO.getString("FOTO");

                icon = LoadImageFromWebOperations(url);

                Team team = new Team(nome, num, data, icon);
                teamAdapter.add(team);

                count++;
            }

        } catch (JSONException e) {
            e.printStackTrace();
            Log.d("Simone", e.toString());
            Log.d("Simone", e.getMessage());
        }
    }

    public static Bitmap LoadImageFromWebOperations() {
        try {
            URL url = new URL("url");
            Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            return bmp;
        } catch (Exception e) {
            Log.d(TAG, "bitmap error: " + e.toString());
            Log.d(TAG, "bitmap error: " + e.getMessage());
            return null;
        }
    }

}
公共类DisplayListView扩展了AppCompative活动{
最终静态字符串TAG=“sb.dl”;
字符串json_字符串;
JSONObject JSONObject;
JSONArray JSONArray;
团队适配器团队适配器;
列表视图列表视图;
位图图标=空;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.display\u listview\u layout);
teamAdapter=新的teamAdapter(这个,R.layout.row\u布局);
listView=(listView)findViewById(R.id.listView);
setAdapter(teamAdapter);
json_string=getIntent().getExtras().getString(“json_数据”);
Log.d(标记,“json_字符串”+json_字符串);
试一试{
jsonObject=新的jsonObject(json_字符串);
jsonArray=jsonObject.getJSONArray(“risposta”);
整数计数=0;
字符串nome,num,data;
while(count
尝试使用毕加索加载图像

这将异步将图像加载到imageview中

使用gradle导入获取库

compile 'com.squareup.picasso:picasso:2.5.2'
我也有同样的问题。 我通过下载位图的异步任务解决了这个问题

我使用了以下代码:

class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {

    private final WeakReference<ImageView> imageViewReference;

    public ImageDownloaderTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        return downloadBitmap(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                } else {
                    Drawable placeholder = null;
                    imageView.setImageDrawable(placeholder);
                }
            }
        }
    }

    private Bitmap downloadBitmap(String url) {
        HttpURLConnection urlConnection = null;
        try {
            URL uri = new URL(url);
            urlConnection = (HttpURLConnection) uri.openConnection();

            final int responseCode = urlConnection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                return null;
            }

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (Exception e) {
            urlConnection.disconnect();
            Log.w("ImageDownloader", "Errore durante il download da " + url);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }
}

希望有帮助:)

你能看看这个问题吗?[如何修复NetworkOnMainThreadException]()。你基本上不想在你的主线程上做任何网络活动。我已经做了。。但是我需要一个提示,请您使用类似于for的东西,因为您不能使用主线程进行网络操作。阅读一些有关AsyncTask的示例或使用凌空库如果我使用AsyncTask,是否可以等待图标出现,然后添加项目?我不需要将图片加载到图像视图,而是加载到一个简单的位图,然后将其传递到列表行。我没有时间将url转换为位图。。我怎么做?是的!非常适合我!我为我的项目调整了它,效果很好!非常感谢
class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {

    private final WeakReference<ImageView> imageViewReference;

    public ImageDownloaderTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        return downloadBitmap(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                } else {
                    Drawable placeholder = null;
                    imageView.setImageDrawable(placeholder);
                }
            }
        }
    }

    private Bitmap downloadBitmap(String url) {
        HttpURLConnection urlConnection = null;
        try {
            URL uri = new URL(url);
            urlConnection = (HttpURLConnection) uri.openConnection();

            final int responseCode = urlConnection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                return null;
            }

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (Exception e) {
            urlConnection.disconnect();
            Log.w("ImageDownloader", "Errore durante il download da " + url);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }
}
new ImageDownloaderTask(holder.imageView).execute(urlPhoto);