当我运行此代码时,我在Input stream in =url.openstream
上收到错误
帮我。解决这个问题
我在清单
中添加了互联网和写入和读取权限我在createmethod上创建了一个 asyncktask 的实例
我在url.openstream
中尝试doInbackground
但仍然收到错误
没有我在Java程序中执行的asyncktask类。但在Android无法正常工作
private class Mine extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPreExecute() {
Toast.makeText(getApplicationContext(),"its loading",Toast.LENGTH_LONG).show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document doc=Jsoup.connect(addr).get();
come=doc.title();
Elements image=doc.getElementsByTag("img");
for(Element thereimage:image)
{
thereimagelink=thereimage.attr("abs:src");
}
int indexname = thereimagelink.lastIndexOf("/");
if(indexname==thereimagelink.length())
{
thereimagelink=thereimagelink.substring(1,indexname);
}
indexname = thereimagelink.lastIndexOf("/");
hereimagename=thereimagelink.substring(indexname,thereimagelink.length());
} catch (IOException e) {
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(),""+come,Toast.LENGTH_SHORT).show();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
try {
URL url=new URL(thereimagelink);
InputStream in=url.openStream();
OutputStream out=new BufferedOutputStream(new FileOutputStream(folder+hereimagename));
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}catch (IOException e)
{
}
Toast.makeText(getApplicationContext(),"its finishd",Toast.LENGTH_LONG).show();
super.onPostExecute(aVoid);
}
答案 0 :(得分:1)
您在url.openStream()
中运行onPostExecute()
,它在UI线程上运行。 Android要求所有网络操作都应该在后台线程中进行,因此所有这些操作都应该在doInBackground()