0

I've made a web service in java which should returns the content of a folder that is in my 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:

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

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 :

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. But I feel like I can't receive the File in my android. 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)

If someone have any idea how I could get the list of the files easier, your welcome :) Where am I wrong?

Purfakt
  • 101
  • 1
  • 16
  • How can a web service know what files are in your PC? unless it is *running* in your PC? In which case why you do need a web service when you could just call `File.listFiles()?` – user207421 Jun 20 '12 at 10:28
  • @EJP Ok after reading your comment again I found out my code isn't good for that line... But anyway, It doesn't explain me the error. I change my code, check out my edited code! – Purfakt Jun 20 '12 at 14:45

1 Answers1

0

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). This link for a similar question on PHP servers lead to This description on how to do it in Java and Jax/RPC

Good luck!

Community
  • 1
  • 1
mezmo
  • 2,441
  • 19
  • 22
  • I'm pretty sc**wd then. I need to get those file on the tablet without plug it in the pc. That's the purpose of the app. And I need to do it before few days. The strange thing is my web service works in a sense. I mean, I can launch it and see the XML on the navigator and I think the problem comes from my receiver code. – Purfakt Jun 20 '12 at 15:04
  • Dude! Don't give up so quick!! You could also switch over to REST, create a bunch of URLs that would serve the files up over HTTP, then retrieve them individually, or just send the urls as a bundle in your soap message and then grab them, but again individually and just via http. – mezmo Jun 21 '12 at 16:15
  • Yeah I heard about REST, I about to try it. I got two weeks to learn it. The thing is I'm a newbie in programing so I feel surrounded by problems. Thanks for your answer and cheer me up by the way :) – Purfakt Jun 22 '12 at 08:15