简体   繁体   English

如何用Java在WebService for Android中返回文件

[英]How to return a File in a WebService for Android in Java

I've made a web service in java which should returns the content of a folder that is in my pc. 我已经在Java中制作了一个Web服务,该服务应该返回我PC中文件夹的内容。 I want to fill a list with the files name and when the user tap one, it will be donwload thane opened by my application. 我想用文件名填充一个列表,当用户点击一个列表时,它将被我的应用程序打开。 For the test, I just want to get at least the files name. 对于测试,我只想至少获得文件名。 I used the folowing code: 我使用以下代码:

SimpleWebService: SimpleWebService:

@WebService
@SOAPBinding(style=Style.RPC)
public interface SimpleService {
   @WebMethod
   File getFiles();
}

SimpleWebServiceImpl: SimpleWebServiceImpl:

@WebService(endpointInterface="com.medex.webServiceMegXsoft.SimpleService")
public class SimpleServiceImpl implements SimpleService{
    public File getFiles() {
        File directory = new 
            File("C:\\Users\\student\\Desktop\\MegXsoftMobile\\");
        return directory;
    }
}

SimpleServicePublisher : SimpleServicePublisher:

public class SimpleServicePublisher {
    public static void main(String[] args) {
        Endpoint.publish("http://192.168.0.58:9000/simple",
                new SimpleServiceImpl());
    }
}

And my receiver : 而我的接收者:

public class Receiver {

private final String NAME_SPACE = "http://192.168.0.71:9000/";
private final String URL = "http://192.168.0.71:9000/simple?wsdl";
private final String SOAP_ACTION = "\"getFiles\"";
private final String METHOD_NAME = "getFiles";
private Object resultsRequestSOAP;

public File getFilesFromXML() {
    SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
    try
    {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        resultsRequestSOAP =  envelope.getResponse();
    } catch (SoapFault e) {
        e.printStackTrace();
    }catch (XmlPullParserException e){
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }

    return (File)resultsRequestSOAP;
}

} }

And finaly the Activity : 最后的活动:

ArrayList<String> stringTable;
File files = null;
ProgressDialog dialog;
Thread thread;
Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    dialog = ProgressDialog.show(this, null, "loading", true, false);

    thread = new Thread(new Runnable() {
        public void run() {
            Receiver receiver = new Receiver();
            files = receiver.getFilesFromXML();
            dialog.dismiss();
        }
    });
    thread.start();

    final Handler h = new Handler();
    h.postDelayed(new Runnable() {

        public void run() {
            if (thread.getState() == Thread.State.TERMINATED) {
                int i = 0;

                stringTable = new ArrayList<String>();

                for (File file : files.listFiles()) {
                    stringTable.add(i, file.getName());
                    i++;
                }
                final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        context, R.layout.item, stringTable);
                ListView listView = getListView();
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent,
                            View view, int position, long id) {
                        System.out.println(stringTable.get(position));
                    }
                });

            }
            h.postDelayed(this, 1000);
        }
    }, 1000);
}

It seems to publish something as when I go to http://192.168.0.58:9000/simple?wsdl I have some lines in XML. 似乎发布了一些内容,因为当我转到http://192.168.0.58:9000/simple?wsdl我在XML中有几行内容。 But I feel like I can't receive the File in my android. 但是我感觉我无法在Android中接收File I got this error: 我收到此错误:

06-20 13:55:55.759: W/System.err(13370): org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG @1:290 in java.io.InputStreamReader@4053a7f0) 06-20 13:55:55.759:W / System.err(13370):org.xmlpull.v1.XmlPullParserException:预期:END_TAG {http://schemas.xmlsoap.org/soap/envelope/}正文(位置:END_TAG java.io.InputStreamReader@4053a7f0中的@ 1:290)

If someone have any idea how I could get the list of the files easier, your welcome :) Where am I wrong? 如果有人对如何简化文件列表有任何想法,欢迎您:)我在哪里错了?

Hmmm, don't know for sure, but I don't believe there is a real way to support a "file" construct in SOAP, but you might be able to construct a message with mime encoded attachment(s). 嗯,不确定,但是我不相信有一种真正的方法来支持SOAP中的“文件”构造,但是您也许能够构造带有mime编码附件的消息。 This link for a similar question on PHP servers lead to This description on how to do it in Java and Jax/RPC 此链接在PHP服务器上引发了类似问题,导致此链接 描述了如何在Java和Jax / RPC中进行操作

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM