Android未从web下载.html
我试图在SD卡中保存一个远程web文件(.html),以便以后可以读取或操作它,但应用程序在启动时崩溃Android未从web下载.html,android,html,save,Android,Html,Save,我试图在SD卡中保存一个远程web文件(.html),以便以后可以读取或操作它,但应用程序在启动时崩溃 public class MainActivity extends ActionBarActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState)
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView webview = new WebView(this);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
setContentView(webview);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file:///android_asset/paginas/index.html");
//This LoadURL it load properly if i comment the img.DownloadFromURL, something is wrong there =/, if downloadfromurl is not commented, it crash!
String url = "http://empregocerto.uol.com.br/info/vagas_por_area.html";
ImageManager img = new ImageManager();
img.DownloadFromUrl(url, "index.txt");
//its ImageManager coz in the example the guy was downloading image, but i want all the html code in a file at my cellphone
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ImageManager {
private final String PATH = "/data/data/com.helloandroid.downloader/"; //put the downloaded file here
public void DownloadFromUrl(String imageURL, String fileName) { //this is the downloader method
try {
URL url = new URL(imageURL); //you can write here any link
File file = new File(fileName);
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
日志CAT错误:
04-02 14:33:24.660:V/WebViewChromium(1370):将铬结合到
主活套活套(主活套,tid 1){b3d8e1c8}04-02 14:33:24.680:
铬(1370):[INFO:library\u loader\u hooks.cc(112)]铬记录
已启用:级别=0,默认详细度=0 04-02 14:33:24.700:
I/BrowserProcessMain(1370):初始化chromium进程,渲染器=0
04-0214:33:24.790:D/(1370):主机连接::获取()新主机
已建立连接0xb7df38f8,tid 1370 04-02 14:33:24.800:
W/chromium(1370):[警告:proxy_service.cc(888)]PAC支持已禁用
因为没有系统实现04-02 14:33:24.980:
D/dalvikvm(1370):所有释放86K的GC,6%释放2889K/3060K,暂停
42ms,总计43ms 04-02 14:33:24.980:I/dalvikvm堆(1370):增长堆
对于635812字节分配04-02 14:33:25.040:
D/dalvikvm(1370):GC_FOR_ALLOC freed您得到的错误是
android.os.NetworkOnMainThreadException
。这意味着您不能在主UI线程上运行网络类型操作。在您的情况下,您正试图通过网络检索web内容
因此,img.DownloadFromUrl(url,“index.txt”)代码>必须在中调用或在单独的线程中运行
有关此错误的完整stackoverflow问题和答案,请参阅