【问题标题】:SMB getinputstream application crashesSMB getinputstream 应用程序崩溃
【发布时间】:2018-02-06 13:03:32
【问题描述】:

我面临着与link 相同的问题,但没有得到正确的回答。我正在使用下面的代码。

 btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        try{
            String url = "smb://DHARMU-PC/" + sharedFolder + "/new plugin.txt";
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null);
            SmbFile sfile = new SmbFile(url,auth);

            if (sfile!=null)
            {

                Toast.makeText(Main2Activity.this,"Not", Toast.LENGTH_SHORT).show();
                sfile.getInputStream();

            }
            else
            {
                Toast.makeText(Main2Activity.this, "Succsess", Toast.LENGTH_SHORT).show();
            }


 } catch (MalformedURLException e) {
                            Toast.makeText(Main2Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                        

                    
                });

当我评论 sfile.getInputStream(); 应用程序运行完美,但 getinputstream 应用程序在 try catch 块中没有任何异常。由于上面提到的问题没有得到回答,所以我再次提出这个问题。

编辑

现在我尝试了异步任务

class CopySMBFile extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... f_url) {
        try {
            String fileContent = "This is a test file";
            try{
                String user = "myusername:mypassword";
                System.out.println("User: " + user);

                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
                String path = "smb://192.168.1.12/k/temp/A.txt";
                System.out.println("Path: " +path);

                SmbFile sFile = new SmbFile(path, auth);
                SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
                sfos.write(fileContent.getBytes());

                System.out.println("Finished attempt to write file");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Connected", e.getMessage());
        }

        return null;
    }

现在使用异步任务,SmbFileOutputStream 正在工作,但仍然显示 getinputstream 的致命错误。

编辑

发现Appcrash背后的原因是networkmainthread异常。现在请指导解决它。

【问题讨论】:

标签: java android smb


【解决方案1】:

我认为NtlmPasswordAuthentication 调用是错误的。它期望字符串为host;user:password。在你的情况下应该是192.168.1.12;myusername:mypassword。您也可以使用构造函数的 3 参数版本:NtlmPasswordAuthentication("192.168.1.12","myusername","mypassword")

【讨论】:

    猜你喜欢
    • 2013-06-29
    • 2014-08-25
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2011-01-01
    • 2012-03-30
    • 2011-06-20
    相关资源
    最近更新 更多