0

I would like to learn how to use webSservices with Android. At the moment, I have a server with the wsdl file and I am able to call a desired Webservice from.

My problem is that I got the response from the server and I would like to extract some informations which are interessant for me, for example the user id or something like that.

Here is my code:

public class TestBookSoapActivity extends Activity {

private static final String SOAP_ACTION = "JournalArticles";
private static final String METHOD_NAME = "JournalArticles";
private static final String NAMESPACE = "http://api.mdpi.com/ws/";
private static final String URL = "http://api.mdpi.com/ws/server.php";


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

private void executeAppelSOAP() {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("JournalID", 1);
    request.addProperty("Volume", 12);
    request.addProperty("Issue", 1);

    SoapSerializationEnvelope enveloppe = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    enveloppe.setOutputSoapObject(request);
    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);             
    try {
        androidHttpTransport.call(SOAP_ACTION, enveloppe);
        Object resultat = enveloppe.getResponse();
        System.out.println(" Journal Articles= " + resultat.toString());
    } catch(Exception e) {
        e.printStackTrace();
        System.out.println("Problem");
    }
}

Could I use the enveloppe.parse() method and if so, how?

Thank you.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • The question is not precise, have you managed to get XML correctly in line Object resultat = enveloppe.getResponse(); or do you get an exception? – Dragan Marjanović Feb 02 '12 at 13:21
  • Hvala Dragane, as you can see in the code, I have the line Object resultat = enveloppe.getResponse(); and that get me the xml file which i can for exemple see on my log file, but the problem is, how to get for exemple the some date from this response. – Milos Cuculovic Feb 02 '12 at 13:25
  • Ok, so you are interesting in parsing an XML inside String. You can use SAX parser (for performances reasons) like in http://stackoverflow.com/questions/5752268/android-parse-xml-from-string-problems (check the first answer). Just use inputSource.setCharacterStream(new StringReader(resultat.toString())); Nema na cemu :-) – Dragan Marjanović Feb 02 '12 at 13:42
  • Oh yes, this is the word I was looking for, parsing.... I will try to do this. – Milos Cuculovic Feb 02 '12 at 13:49
  • For the moment, i can't find the solution becuse on the exemple they are using a xml file and i have the ksoap result. – Milos Cuculovic Feb 02 '12 at 16:06

1 Answers1

1

There are two steps you should take:

  1. Get the correct XML value
  2. Parse XML

About 1...
I prefer HttpTransportSE so instead of:

AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);

use:

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

Then instead of:

Object resultat = enveloppe.getResponse();    

use:

SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String xml = response.toString();

You can also check with the debugger how response looks like. More details you can read in answers to this question.

About 2...
You can use any XML parser. I prefer SAX for performance reasons. Here is an Android example: how to parse XML in Android

Regards,
Dragan

Community
  • 1
  • 1
Dragan Marjanović
  • 2,386
  • 1
  • 19
  • 17