[英]Using JCIFS in Android
Here is the case: I want to authenticate my Android device in code, by sending username and password to specific URL, this URL will have internal and external Access. 就是这种情况:我想通过代码验证我的Android设备,方法是将用户名和密码发送到特定的URL,此URL将具有内部和外部访问权限。 through reading JCIFS Library which main job is to connect to windows using NTML, trying to make it works in android, I inserted this code:
通过阅读JCIFS库 ,其主要工作是使用NTML连接到Windows,试图使其在android中工作,我插入了以下代码:
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
String user = "username";
String pass = "password";
String sharedFolder = "shared";
String url = "smb://url/url//" + sharedFolder + "/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
null, user, pass);
SmbFile sfile = new SmbFile(url, auth);
sfile.canRead();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
this sample code I have used for only getting authorization from Windows into my Android application, I just want to return the Auth, not copying files nor comparing between files. 此示例代码仅用于从Windows获得授权进入我的Android应用程序,我只想返回Auth,而不是复制文件或在文件之间进行比较。 I have failed after reading UrlConnection and UrlStreamHandler and have tried almost all questions in StackOverFlow but I didn't find an answer.
在阅读UrlConnection和UrlStreamHandler之后,我失败了,并尝试了StackOverFlow中的几乎所有问题,但没有找到答案。
also when I apply this code, the message that comes is 当我应用此代码时,出现的消息是
02-21 04:50:02.329 3032-3046/ils.com.ntmlproject W/System.err: jcifs.smb.SmbException: Failed to connect to server
02-21 04:50:02.329 3032-3046/ils.com.ntmlproject W/System.err: java.net.UnknownHostException: jorphp01
02-21 04:50:02.329 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.UniAddress.getAllByName(UniAddress.java:315)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:864)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.connect(SmbFile.java:954)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.queryPath(SmbFile.java:1338)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.exists(SmbFile.java:1420)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.canRead(SmbFile.java:1462)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:51)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:39)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.lang.Thread.run(Thread.java:841)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.connect0(SmbFile.java:882)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.queryPath(SmbFile.java:1338)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.exists(SmbFile.java:1420)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at jcifs.smb.SmbFile.canRead(SmbFile.java:1462)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:51)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at ils.com.ntmlproject.MainActivity$1.doInBackground(MainActivity.java:39)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-21 04:50:02.339 3032-3046/ils.com.ntmlproject W/System.err: at java.lang.Thread.run(Thread.java:841)
I don't know if the error is in http protocol ? 我不知道该错误是否在http协议中? or in the code?
还是在代码中? what I need to adjust here and what I have missed?
我需要在这里调整什么,错过什么?
The host name: jorphp01
is not resolvable from the device where the code is executed. 主机名:
jorphp01
无法在执行代码的设备上解析。 Below would be the possible solutions: 以下是可能的解决方案:
I found what case the error and what the solution to my case. 我发现了什么情况下的错误以及我的情况下的解决方案。 first thanks for @Pavan Kumar for answer the question and gives me some hints.
首先感谢@Pavan Kumar回答了这个问题,并给了我一些提示。
I have applied the OkHTTP in network connection using jcifs library to connect to my server through this method which works fine in my case: 我已经使用jcifs库在网络连接中应用了OkHTTP通过这种方法连接到服务器,这种方法在我的情况下效果很好:
final TextView textView = (TextView) findViewById(R.id.tvResponse); new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { HttpUrl.Builder urlBuilder = HttpUrl.parse("http://10.10.6.4/erpapi/").newBuilder(); String url = urlBuilder.build().toString(); // issue the Get request String getResponse = ""; try { getResponse = doGetRequest(url); System.out.println(getResponse); } catch (IOException e) { e.printStackTrace(); getResponse = e.getMessage(); } return getResponse; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); textView.setText(s.toString().toLowerCase()); } }.execute();
and the function is : 函数是:
static String doGetRequest(String url) throws IOException {
okHttpClient = new OkHttpClient.Builder()
.authenticator(new NTMLAuthenticator("username", "password"))
.build();
Request request = new Request.Builder()
.url(url)
.build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();}
the source of this code , and have used the jcifs library for authentication. 此代码的源 ,并已使用jcifs库进行身份验证。
after this I could easily login in to server and sent authenticated. 在此之后,我可以轻松登录服务器并发送经过身份验证的信息。
also these are some useful resources to see from: 这些也是一些有用的资源,可从以下资源中看到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.