问题
I am trying to log on to my windows server to download a file using jcifs. The app works fine on my android 2.2 phone and an old 2.3 tablet. I just purchased a Le Pan II which I upgraded to 4.0.3. When I try to run my app it's crashing when trying to access the server. Is there anything new I need to do for 4.0.3? I tried commenting out everything pertaining to the output stream to make sure it was the input side causing the problem. If I comment out everything inside the innermost try block everything else works fine so it's something inside there. As soon as I uncomment InputStream in = file.getInputStream(); then it crashes.
Permissions in manifest file.
<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"/>
Download function
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";
}
Errors
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
来源:https://stackoverflow.com/questions/12064156/android-smb-file-access-crashing