我正在从JSON文档提供的url下载图像。起初我的应用程序似乎正常工作,拉入并放置图像并捕获异常,当数组元素中没有图像网址但突然崩溃,我的错误日志显示的东西调整到 引起:java.lang.RuntimeException:java.net.MalformedURLException:找不到协议: 问题是我已经发现了这个错误,如下所示。
如果有人能向我解释为什么会发生这种情况并指出我正确的方向,我将非常感激。
Image DwnLdr class
public Drawable loadImage (BaseAdapter adapt, ImageView view)
{
this.adapter = adapt;
String url = (String) view.getTag();
if (imageCache.containsKey(url))
{
return imageCache.get(url);
}
else {
new ImageTask().execute(url);
return DEFAULT_ICON;
}
}
private class ImageTask extends AsyncTask<String, Void, Drawable>
{
private String s_url;
@Override
protected Drawable doInBackground(String... params) {
s_url = params[0];
InputStream inStream;
try {
Log.v(debugTag, "Fetching: " + s_url);
URL url = new URL(s_url);
inStream = url.openStream();
} catch (MalformedURLException e) {
Log.v(debugTag, "Malformed: " + e.getMessage());
throw new RuntimeException(e);
} catch (IOException e)
{
Log.d(debugTag, "I/O : " + e.getMessage());
throw new RuntimeException(e);
}
return Drawable.createFromStream(inStream, "src");
}
@Override
protected void onPostExecute(Drawable result) {
super.onPostExecute(result);
synchronized (this) {
imageCache.put(s_url, result);
}
adapter.notifyDataSetChanged();
}
}
查看适配器类
ListData data = topics.get(position);
try {
long lg = Long.valueOf(data.getPostTime())*1000;
Date date = new Date(lg);
String postTime = new SimpleDateFormat("MM dd, yyyy hh:mma").format(date);
holder.data = data;
holder.listName.setText(data.getTitle());
holder.authorName.setText(data.getAuthor());
holder.postTime.setText(postTime);
holder.redditScore.setText(data.getrScore());
Log.v(DEBUG_TAG, "Cell Created");
}catch (Exception e){
e.printStackTrace();
Log.v(DEBUG_TAG,"Cell Not Created Due to: ",e);
}
if(data.getImageUrl()!=null){
try {
holder.thumbnail.setTag(data.getImageUrl());
Drawable drawable = imgGet.loadImage(this, holder.thumbnail);
if (drawable != null) {
holder.thumbnail.setImageDrawable(drawable);
} else {
holder.thumbnail.setImageResource(R.drawable.filler_icon);
}
}catch (Exception e){
e.printStackTrace();
Log.v(DEBUG_TAG,"no image: ",e);
}
return convertView;
}
主类适配器集
public static class MyViewHolder {
public TextView listName, authorName, redditScore, postTime;
public Button goButton;
public ImageView thumbnail;
public ListData data;
}
public void setTopics(ArrayList<ListData> data) {
this.data = data;
this.postList.setAdapter(new RedditDataAdapter(this, this.getImg, this.layoutInflater,this.data));
}
错误记录
Caused by: java.lang.RuntimeException: java.net.MalformedURLException: Protocol not found:
at Tasks.RedditIconTask$ImageTask.doInBackground(RedditIconTask.java:60)
at Tasks.RedditIconTask$ImageTask.doInBackground(RedditIconTask.java:46)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
答案 0 :(得分:0)
看起来图片网址是一个空字符串。调试代码以了解其原因。您发布的代码并未真正显示URL值的来源。
为什么你得到异常是因为你重新抛出它,包裹在RuntimeException
中:
throw new RuntimeException(e);
答案 1 :(得分:0)
我能够通过使用laatlto所说的内容并将代码更改为以下内容来解决我的问题。
protected Drawable doInBackground(String... params) {
s_url = params[0];
InputStream inStream;
Drawable picture=null;
try {
Log.v(debugTag, "Fetching: " + s_url);
URL url = new URL(s_url);
inStream = url.openStream();
picture= Drawable.createFromStream(inStream, "src");
} catch (MalformedURLException e) {
Log.v(debugTag, "Malformed: " + e.getMessage());
} catch (IOException e)
{
Log.d(debugTag, "I/O : " + e.getMessage());
}
return picture;
}