首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过web服务将文件(至少11个文件)从文件夹发送到android应用程序

通过web服务将文件(至少11个文件)从文件夹发送到android应用程序
EN

Stack Overflow用户
提问于 2010-12-28 09:22:30
回答 1查看 1.7K关注 0票数 2

我陷入了这种困境,请帮帮我。

我的问题是,我想使用web服务将文件(总共11个PDF文件)发送到android应用程序。

我尝试使用下面的代码.主类创建服务

代码语言:javascript
复制
public class MultipleFilesImpl implements MultipleFiles {

public FileData[] sendPDFs() {
    FileData fileData = null;
    // List<FileData> filesDetails = new ArrayList<FileData>();
    File fileFolder = new File(
            "C:/eclipse/workspace/AIPWebService/src/pdfs/");
    // File fileTwo = new File(
    // "C:/eclipse/workspace/AIPWebService/src/simple.pdf");

    File sendFiles[] = fileFolder.listFiles();
    // sendFiles[0] = fileOne;
    // sendFiles[1] = fileTwo;

    DataHandler handler = null;
    char[] readLine = null;
    byte[] data = null;
    int offset = 0;
    int numRead = 0;
    InputStream stream = null;
    FileOutputStream outputStream = null;
    FileData[] filesData = null;
    try {
        System.out.println("Web Service Called Successfully");

        for (int i = 0; i < sendFiles.length; i++) {
            handler = new DataHandler(new FileDataSource(sendFiles[i]));
            fileData = new FileData();
            data = new byte[(int) sendFiles[i].length()];
            stream = handler.getInputStream();
            while (offset < data.length
                    && (numRead = stream.read(data, offset, data.length
                            - offset)) >= 0) {
                offset += numRead;
            }
            readLine = Base64Coder.encode(data);
            offset = 0;
            numRead = 0;
            System.out.println("'Reading File............................");
            System.out.println("\n");
            System.out.println(readLine);
            System.out.println("Data Reading Successful");

            fileData.setFileName(sendFiles[i].getName());
            fileData.setFileData(String.valueOf(readLine));

            readLine = null;
            System.out.println("Data from bean " + fileData.getFileData());

            outputStream = new FileOutputStream("D:/"
                    + sendFiles[i].getName());

            outputStream.write(Base64Coder.decode(fileData.getFileData()));

            outputStream.flush();
            outputStream.close();
            stream.close();
            // FileData fileDetails = new FileData();
            // fileDetails = fileData;
            // filesDetails.add(fileData);
            filesData = new FileData[(int) sendFiles[i].length()];
        }
        // return fileData;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return filesData;
}

}

此外,接口多文件:-

代码语言:javascript
复制
public interface MultipleFiles extends Remote {
    public FileData[] sendPDFs() throws FileNotFoundException, IOException,
            Exception;
}

在这里,我发送了一个数组的bean“文件数据”,具有属性即。FileData & FileName.FileData-包含编码的文件数据。文件名-编码的文件名。

豆子:- (FileData)

代码语言:javascript
复制
public class FileData {
    private String fileName;
    private String fileData;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileData() {
        return fileData;
    }

    public void setFileData(String string) {
        this.fileData = string;
    }

}

当在下面的代码中尝试时,android会释放出内存异常&当我试图发送两个文件时,只创建第一个文件。

代码语言:javascript
复制
public class PDFActivity extends Activity {
    private final String METHOD_NAME = "sendPDFs";
    private final String NAMESPACE = "http://webservice.uks.com/";
    private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
    private final String URL = "http://192.168.1.123:8080/AIPWebService/services/MultipleFilesImpl";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView textViewOne = (TextView) findViewById(R.id.textViewOne);

        try {
            SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.setOutputSoapObject(soapObject);

            textViewOne.setText("Web Service Started");
            AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);

            httpTransport.call(SOAP_ACTION, envelope);

            // SoapObject result = (SoapObject) envelope.getResponse();
            Object result = envelope.getResponse();
            Log.i("Result", result.toString());
            // String fileName = result.getProperty("fileName").toString();
            // String fileData = result.getProperty("fileData").toString();

            // Log.i("File Name", fileName);
            // Log.i("File Data", fileData);

            // File pdfFile = new File(fileName);


            // FileOutputStream outputStream =
            // openFileOutput(pdfFile.toString(),
            // MODE_PRIVATE);

            // outputStream.write(Base64Coder.decode(fileData));
            Log.i("File", "File Created");
            // textViewTwo.setText(result);
            // Object result = envelope.getResponse();
            // FileOutputStream outputStream = openFileOutput(name, mode)

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

请帮助解释或更改我的代码。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-30 09:51:53

我自己解决了这个问题。

使用kSoap2 API2.4版本所面临的主要问题是我无法获得复杂的对象(在我的例子中是bean数组)。

然后,我修改了代码(使用Ver2.4):-

代码语言:javascript
复制
try {
            SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.setOutputSoapObject(soapObject);

            textViewOne.setText("Web Service Started");
            AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);

            httpTransport.call(SOAP_ACTION, envelope);

            // SoapObject result = (SoapObject) envelope.getResponse();
            Object result = envelope.getResponse();
            Log.i("Result", result.toString());
            // String fileName = result.getProperty("fileName").toString();
            // String fileData = result.getProperty("fileData").toString();

            // Log.i("File Name", fileName);
            // Log.i("File Data", fileData);

            // File pdfFile = new File(fileName);


            // FileOutputStream outputStream =
            // openFileOutput(pdfFile.toString(),
            // MODE_PRIVATE);

            // outputStream.write(Base64Coder.decode(fileData));
            Log.i("File", "File Created");
            // textViewTwo.setText(result);
            // Object result = envelope.getResponse();
            // FileOutputStream outputStream = openFileOutput(name, mode)

        } catch (Exception e) {
            e.printStackTrace();
        }

(使用ver2.5.1):-

代码语言:javascript
复制
    try {
        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.setOutputSoapObject(soapObject);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);

        SoapObject result = (SoapObject) envelope.bodyIn;

        for (int i = 0; i < result.getPropertyCount(); i++) {
            SoapObject object = (SoapObject) result.getProperty(i);

            Log.i("File Data", object.getProperty("fileData").toString());
            Log.i("File Name", object.getProperty("fileName").toString());

            pdfFiles = new File(object.getProperty("fileName").toString());
            outputStream = openFileOutput(pdfFiles.toString(), MODE_PRIVATE);
            outputStream.write(Base64Coder.decode(object.getProperty(
                    "fileData").toString()));
        }

        outputStream.flush();
        outputStream.close();
        Log.d("File Creation Message", "Files Created Succesfully");
    } catch (SoapFault fault) {
        fault.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (outputStream != null) {
            outputStream = null;
        }
    }

所以我找到了解决办法。

主要的帮助是通过这个问题的24 :-请阅读如果你正面临这样的问题。链接-问题-24

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4544965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档