我正在尝试登录到我的 Windows 服务器以使用 jcifs 下载文件。该应用程序在我的 android 2.2 手机和旧的 2.3 平板电脑上运行良好。我刚刚购买了一台 Le Pan II,我升级到了 4.0.3。当我尝试运行我的应用程序时,它在尝试访问服务器时崩溃。我需要为 4.0.3 做些什么新的事情吗?我尝试注释掉与输出流有关的所有内容,以确保它是导致问题的输入端。如果我注释掉最里面的所有内容,try block 其他所有内容都可以正常工作,所以它就在里面。一旦我取消注释InputStream in = file.getInputStream(); 然后它崩溃了。
清单文件中的权限。
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>
下载功能
public static String DlFile() {
SmbFile file = null;
try {
file = new SmbFile("smb://NAME:PASSWORD@10.0.0.4/jws/Data/Inspection Reports/forms/0990125.txt");
Log.v(TAG, "smbFile: " + file.toString());
try {
InputStream in = file.getInputStream();
FileOutputStream out = new FileOutputStream(cfgSDFolder + myFile);
byte[] b = new byte[8192];
int n;
while(( n = in.read( b )) > 0 ) {
out.write( b, 0, n );
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
Log.v(TAG, "DSZerror: " + e.getMessage().toString());
return "error: " + e.getMessage().toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.v(TAG, "DSZerror:" + e.getMessage().toString());
return "error: " + e.getMessage().toString();
} // note the different format
return "ok";
}
错误
08-21 18:39:50.548: W/dalvikvm(4604): Exception Landroid/os/NetworkOnMainThreadException; thrown while initializing Ljcifs/netbios/NbtAddress;
08-21 18:39:50.548: W/dalvikvm(4604): Exception Ljava/lang/ExceptionInInitializerError; thrown while initializing Ljcifs/UniAddress;
08-21 18:39:50.548: W/dalvikvm(4604): threadid=1: thread exiting with uncaught exception (group=0x40a871f8)
08-21 18:39:50.578: E/AndroidRuntime(4604): FATAL EXCEPTION: main
08-21 18:39:50.578: E/AndroidRuntime(4604): java.lang.ExceptionInInitializerError
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFile.connect(SmbFile.java:951)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFile.open0(SmbFile.java:972)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFile.open(SmbFile.java:1006)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.smb.SmbFile.getInputStream(SmbFile.java:2844)
08-21 18:39:50.578: E/AndroidRuntime(4604): at com.thedsz.jws.qc.DSZ.DlFile(DSZ.java:58)
08-21 18:39:50.578: E/AndroidRuntime(4604): at com.thedsz.jws.qc.Splash$2.afterTextChanged(Splash.java:99)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.widget.TextView.sendAfterTextChanged(TextView.java:7655)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8057)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:900)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:117)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.widget.TextView.doKeyDown(TextView.java:5445)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.widget.TextView.onKeyDown(TextView.java:5267)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.KeyEvent.dispatch(KeyEvent.java:2566)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.View.dispatchKeyEvent(View.java:5500)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
08-21 18:39:50.578: E/AndroidRuntime(4604): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
08-21 18:39:50.578: E/AndroidRuntime(4604): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
08-21 18:39:50.578: E/AndroidRuntime(4604): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2597)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.os.Looper.loop(Looper.java:137)
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-21 18:39:50.578: E/AndroidRuntime(4604): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 18:39:50.578: E/AndroidRuntime(4604): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 18:39:50.578: E/AndroidRuntime(4604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
08-21 18:39:50.578: E/AndroidRuntime(4604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
08-21 18:39:50.578: E/AndroidRuntime(4604): at dalvik.system.NativeStart.main(Native Method)
08-21 18:39:50.578: E/AndroidRuntime(4604): Caused by: java.lang.ExceptionInInitializerError
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.UniAddress.<clinit>(UniAddress.java:62)
08-21 18:39:50.578: E/AndroidRuntime(4604): ... 43 more
08-21 18:39:50.578: E/AndroidRuntime(4604): Caused by: android.os.NetworkOnMainThreadException
08-21 18:39:50.578: E/AndroidRuntime(4604): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-21 18:39:50.578: E/AndroidRuntime(4604): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-21 18:39:50.578: E/AndroidRuntime(4604): at java.net.InetAddress.getLocalHost(InetAddress.java:371)
08-21 18:39:50.578: E/AndroidRuntime(4604): at jcifs.netbios.NbtAddress.<clinit>(NbtAddress.java:187)
08-21 18:39:50.578: E/AndroidRuntime(4604): ... 44 more