我正在尝试将搜索查询中的推文信息检索到我的 android 应用程序中。我可以将用户名和主题放在列表视图中,但我无法抓取照片。这是我的适配器的 GetView 方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
UserRecord user = users.get(position);
if (user != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView subject = (TextView) v.findViewById(R.id.subject);
// Bitmap bitmap = DownloadImage(user.imageURL);
ImageView img = (ImageView) v.findViewById(R.id.avatar);
if (username != null) {
username.setText(user.username);
}
if(subject != null) {
subject.setText(user.subject);
}
if (img != null){
try {
URL myURL = new URL(user.imageURL);
HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
img.setImageBitmap(BitmapFactory.decodeStream(is));
} catch (Exception e) {
e.printStackTrace();
}
}
}
return v;
}
我跟踪了我的代码,它在 conn.connect() 上失败并跳转到 catch 语句。有谁知道为什么会这样?我在 mainfest 中启用了 Internet 连接。谢谢!