文件下载Web服务Android客户端

时间:2011-10-14 12:22:30

标签: android web-services axis2

我写了一个接受肥皂需求的axis2网络服务。

网络服务部分:

 public DataHandler getFile(String fileName)
  {
    FileDataSource dataSource = new FileDataSource(writePath + "/" + fileName);
    DataHandler fileDataHandler = new DataHandler(dataSource);
    return fileDataHandler;
  }

使用datahandler的普通java客户端工作正常。但是我想编写一个用于检索数据的android客户端。我知道android没有javax.activation.DataHandler实现。在Axis2中,我试图让服务器接收http请求以充当休息服务。

services.xml配置

<service name="WsFileTransfer">
<description>
    Please Type your service description here
</description>
<messageReceivers>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
        class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
        class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<parameter name="ServiceClass" locked="false">com.company.project.web.service.WsFileTransfer
</parameter>
<operation name="getFile">

    <parameter name="RESTMethod">GET</parameter>

    <parameter name="RESTLocation">getFile/{fileName}</parameter>

</operation>

android客户端看起来像这样:

客户端

private void getFile(String fileName) throws JSONException, FileNotFoundException {


    client.getParams().setParameter(ClientPNames.COOKIE_POLICY,
            CookiePolicy.RFC_2109);
    StringEntity tmp = null;

    httpGet = new HttpGet(EPR + "getFile?fileName=" + fileName);
    httpGet.setHeader("Content-Type", CONTENT_TYPE);


    FileOutputStream stream = null;
    InputStream readStream = null;
    try {
        response = client.execute(httpGet);


        HttpEntity returnEntity = response.getEntity();
        readStream = returnEntity.getContent();

    } catch (ClientProtocolException e) {

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

        e.printStackTrace();
    }

    String filePath = Environment.getExternalStorageDirectory().getPath()
    + System.getProperty("file.separator") + fileName;
    File file = new File(filePath);
    stream = new FileOutputStream(file);


    try {
        while(readStream.read() >= 0){
        stream.write(readStream.read());
        }
    } catch (IOException e) {

        e.printStackTrace();
    }
    finally{

        if(stream != null)
            try {
                stream.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        if(readStream != null){
            try {
                readStream.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

}

最后我收到了一些数据,但它们与实际数据不一致。在哪一方犯了错误?

0 个答案:

没有答案