我有如何在我的应用程序上下载图像的功能,但我不知道为什么我在主线程异常上收到了该网络。 这是我的功能类:
public class Tail {
String story, description, title, location;
int imageID;
URL url;
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public Tail(int imageID, String location, String description, String title, String story) {
this.imageID = imageID;
this.location = location;
this.title = title;
this.description = description;
this.story = story;
}
public Tail(String url, String location, String description, String title, String story) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
this.location = location;
this.title = title;
this.description = description;
this.story = story;
}
public String getStory() {
return story;
}
public void setStory(String story) {
this.story = story;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getImageID() {
return imageID;
}
public void setImageID(int imageID) {
this.imageID = imageID;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public static Bitmap getImageBitmapFromUrl(URL url) {
Bitmap bm = null;
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() != 200) {
return bm;
}
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
try {
bm = BitmapFactory.decodeStream(bis);
} catch (OutOfMemoryError ex) {
bm = null;
}
bis.close();
is.close();
} catch (Exception e) {
}
return bm;
}
知道任何人如何解决这个问题?
答案 0 :(得分:0)
当应用程序尝试在其主线程上执行网络操作时,抛出此异常。
您需要在asynctask中执行HTTP连接。 看看如何使用assynctask http://developer.android.com/reference/android/os/AsyncTask.html