Java 参数从被调用方传递,但在方法定义中为null
我基本上是在尝试创建一个解压函数。我调用了下面块中带有参数的函数:Java 参数从被调用方传递,但在方法定义中为null,java,android,oop,Java,Android,Oop,我基本上是在尝试创建一个解压函数。我调用了下面块中带有参数的函数: UnzipUtility unzipUtility = new UnzipUtility(); try { unzipUtility.unzip(localFilePath, parentPath); } catch (IOException e) { e.printStackTrace(); } 该方法的定义在一个类unziputability中,代码如下: p
UnzipUtility unzipUtility = new UnzipUtility();
try {
unzipUtility.unzip(localFilePath, parentPath);
} catch (IOException e) {
e.printStackTrace();
}
该方法的定义在一个类unziputability中,代码如下:
public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
package com.example.sftpconnection;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
//import java.io.BufferedOutputStream;
import java.io.File;
//import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.OutputStream;
import java.util.List;
import com.example.sftpconnection.UnzipUtility;
public class SFTPActivity extends AppCompatActivity {
private String fileName = "1234.zip";
private String localFilePath;
private String parentPath;
@SuppressLint("StaticFieldLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sftp);
new AsyncTask<Void, Void, List<String>>() {
@Override
protected List<String> doInBackground(Void... params) {
try {
Downloader(fileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
UnzipUtility unzipUtility = new UnzipUtility();
try {
unzipUtility.unzip(localFilePath, parentPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public void Downloader(String fileName) {
String user = "1234";
String pass = "1234";
String host = "1234";
int portNum = 22;
JSch jsch = new JSch();
Session session;
try {
session = jsch.getSession(user,host,portNum);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
File localFile = File.createTempFile("1234",".zip");
sftpChannel.get(fileName,localFile.getAbsolutePath());
//sftpChannel.get(fileName);
Log.d(fileName, " has been downloaded");
sftpChannel.exit();
session.disconnect();
localFilePath = localFile.getAbsolutePath();
parentPath = localFile.getParent();
} catch (JSchException | IOException | SftpException e) {
e.printStackTrace();
}
}
}
但在运行时,尽管参数在主类中正确传递,但值在解压方法中显示为null
请帮帮忙
主要类别如下:
public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
package com.example.sftpconnection;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
//import java.io.BufferedOutputStream;
import java.io.File;
//import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.OutputStream;
import java.util.List;
import com.example.sftpconnection.UnzipUtility;
public class SFTPActivity extends AppCompatActivity {
private String fileName = "1234.zip";
private String localFilePath;
private String parentPath;
@SuppressLint("StaticFieldLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sftp);
new AsyncTask<Void, Void, List<String>>() {
@Override
protected List<String> doInBackground(Void... params) {
try {
Downloader(fileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
UnzipUtility unzipUtility = new UnzipUtility();
try {
unzipUtility.unzip(localFilePath, parentPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public void Downloader(String fileName) {
String user = "1234";
String pass = "1234";
String host = "1234";
int portNum = 22;
JSch jsch = new JSch();
Session session;
try {
session = jsch.getSession(user,host,portNum);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
File localFile = File.createTempFile("1234",".zip");
sftpChannel.get(fileName,localFile.getAbsolutePath());
//sftpChannel.get(fileName);
Log.d(fileName, " has been downloaded");
sftpChannel.exit();
session.disconnect();
localFilePath = localFile.getAbsolutePath();
parentPath = localFile.getParent();
} catch (JSchException | IOException | SftpException e) {
e.printStackTrace();
}
}
}
package com.example.sftpconnection;
导入android.annotation.SuppressLint;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入com.jcraft.jsch.Channel;
导入com.jcraft.jsch.ChannelSftp;
导入com.jcraft.jsch.jsch;
导入com.jcraft.jsch.Session;
导入com.jcraft.jsch.JSchException;
导入com.jcraft.jsch.SftpException;
//导入java.io.BufferedOutputStream;
导入java.io.File;
//导入java.io.FileOutputStream;
导入java.io.IOException;
//导入java.io.OutputStream;
导入java.util.List;
导入com.example.sftpconnection.unziputability;
公共类SFTPActivity扩展了AppCompativity{
私有字符串fileName=“1234.zip”;
私有字符串localFilePath;
私有字符串父路径;
@SuppressLint(“StaticFieldLeak”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sftp);
新建异步任务(){
@凌驾
受保护列表doInBackground(无效…参数){
试一试{
下载程序(文件名);
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
}.execute();
unziputability unziputability=新的unziputability();
试一试{
解压(localFilePath,parentPath);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共无效下载程序(字符串文件名){
字符串user=“1234”;
字符串pass=“1234”;
字符串host=“1234”;
int-portNum=22;
JSch JSch=新的JSch();
会议;
试一试{
session=jsch.getSession(用户、主机、端口号);
session.setConfig(“StrictHostKeyChecking”、“no”);
session.setPassword(pass);
session.connect();
Channel=session.openChannel(“sftp”);
channel.connect();
ChannelSftp sftpChannel=(ChannelSftp)信道;
File localFile=File.createTempFile(“1234”、“.zip”);
获取(文件名,localFile.getAbsolutePath());
//sftpChannel.get(文件名);
Log.d(文件名“已下载”);
sftpChannel.exit();
session.disconnect();
localFilePath=localFile.getAbsolutePath();
parentPath=localFile.getParent();
}捕获(JSCHEException | IOException | SftpException e){
e、 printStackTrace();
}
}
}
出于安全原因,我编辑了数据字段。可能会剪切以下行:
UnzipUtility unzipUtility = new UnzipUtility();
try {
unzipUtility.unzip(localFilePath, parentPath);
} catch (IOException e) {
e.printStackTrace();
}
在classSFTPActivity中的下一行下方粘贴:
Downloader(fileName);
方法doInBackground
。
实际上,方法
doInBackground()
在与方法onCreate
运行的线程不同的线程中运行。在方法Downloader(fileName)
完成其工作之前,尝试使用变量。这就是为什么在变量中看到空值的原因,如:localFilePath
和parentPath
代码不工作的原因是因为在运行此行时:
unzipUtility.unzip(localFilePath, parentPath);
尚未设置变量localFilePath
和parentPath
您可能会争辩说它们是在方法Downloader
中设置的,该方法在解压
行之前调用。不幸的是,事实并非如此。在这种情况下,代码执行不是线性的,因为您使用的是AsyncTask
。异步任务中的内容与后面的行同时运行
您的下载调用不会在解压之前完成。会调用解压,因为与创建新的解压对象相比,下载需要花费大量时间
这就是为什么localFilePath
和parentPath
为空
解决此问题的一种方法是将解压逻辑也移动到异步任务中:
new AsyncTask<Void, Void, List<String>>() {
@Override
protected List<String> doInBackground(Void... params) {
try {
Downloader(fileName);
UnzipUtility unzipUtility = new UnzipUtility();
unzipUtility.unzip(localFilePath, parentPath);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
你在哪里得到空值?发布错误日志你能显示主类吗?你所说的是不可能的。你肯定传错了什么东西。尝试调试。@sasikumar我在调试期间在解压方法定义中得到了空值。非常感谢。这成功了!:)谢谢你的解释。我很想提高投票率,但我没有足够的声誉!:(对不起,还有其他方法吗?好的。谢谢你的详细解释。我下次会记住这些要点。