正在将文件发送到另一个 class 中的服务器

Sending file to server in another class

我想通过 http post 在名为 Add 的 class 中发送一个字节数组的文件。

在名为 FileChooserActivity(扩展 FragmentActivity)的 class 中,我有一个方法 select 名为 onFileSelected() 的文件:

@Override
    public void onFileSelected(File file) {
        if (file != null) {
            if (file.isDirectory()) {
                replaceFragment(file);
            } else {
                finishWithResult(file);

                setFile(file);

                FileUtils futils = new FileUtils();
                try {
                    futils.fullyReadFileToBytes(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Log.v("file", file.toString());
                Toast.makeText(FileChooserActivity.this, "File uploaded", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(this,AddAlerts.class);
                intent.putExtra("file", file);
                startActivity(intent);

            }
        } else {
            Toast.makeText(FileChooserActivity.this, "Error selecting File", Toast.LENGTH_SHORT).show();
        }
    }

    public File setFile(File file){
        return file;
    }

在名为 FileUtils 的 class 中,我有一个方法可以将文件转换为名为 fullyReadFileToBytes 的字节数组:

 public byte[] fullyReadFileToBytes(File file) throws IOException {
        Log.v("fullyReadFileToBytes", file.toString());

        int size = (int) file.length();
        byte bytes[] = new byte[size];
        byte tmpBuff[] = new byte[size];
        FileInputStream fis= new FileInputStream(file);
        try {

            int read = fis.read(bytes, 0, size);
            if (read < size) {
                int remain = size - read;
                while (remain > 0) {
                    read = fis.read(tmpBuff, 0, remain);
                    System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
                    remain -= read;
                }
            }
        }  catch (IOException e){
            throw e;
        } finally {
            fis.close();
        }
        Log.v("bytes[]", bytes.toString());
        return bytes;
    }

在名为 WebConnection 的 class 中,我执行发送文件所需的所有 http post 方法。

关于如何在 Add class 中调用 FileChooserActivity 的 onFileSelected() 方法以通过 http post 发送文件(字节数组形式)的任何建议?

有想法但没有成功,因为文件没有从 FileChooserActivity 中正确 grabbed/referenced 所以它是空的:

在Add.class中:

FileChooserActivity fca = new FileChooserActivity();
               File file = null;
               fca.onFileSelected(file);

               Log.v("file", file.toString());

               conn.addValuePair("a_file[]", file.toString());

正如 nochindeluxe 所说,您不应该实例化一个 activity 来简单地调用一个方法。

按照他的说法,在您的 activity 中添加一个接口,允许您设置侦听器回调,该接口将在操作完成时 运行。您可以创建一个独立的侦听器接口或在 activity class 中添加一个作为内部 class。我不太确定这里发生的事情的顺序,但这应该让你知道该怎么做。

public interface OnFileAddedListener {
    // your method proto here. 
    void onFileAdded(Byte[] theFile);
} 

现在在 class 中创建一个成员变量,您希望回调来自 运行。

private OnFileAddedListener mListener;

为监听器创建一个setter。

public void setOnFileAddedListener(OnFileAddedListener listener)  {
    mListener = listener;
} 

现在,无论您希望在何处执行此方法(例如,在添加文件后),您都应该在何处执行回调。

if (mListener != null) {
    mListener.onFileAdded(theByteArray);
} 

现在,当您创建对象时,您可以使用 activity 中的类似内容调用 setter。

YourClass theObject = new YourClass();

// Set the call back from your activity. 
theObject.setOnFileAddedListener(new OnFileAddedListener()  {
    @Override
    public void onFileAdded(Byte[] theFile) {
        // Send your file here, in the activity, via the callback listener, using your asynctask. 
    } 
} 

希望这对您有所帮助。