-1

I'm actually using a WebService from W3School for testing, and I have this method:

public static float getCelsius(int Fahrenheit)
{
    String SOAP_ACTION = "http://www.w3schools.com/xml/FahrenheitToCelsius";
    String METHOD_NAME = "FahrenheitToCelsius";
    String NAMESPACE = "http://www.w3schools.com/xml/";
    String URL = "http://www.w3schools.com/xml/tempconvert.asmx";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("Fahrenheit", Fahrenheit);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    try {
        HttpTransportSE transport = new HttpTransportSE(URL);
        transport.call(SOAP_ACTION, envelope);

        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
        return Float.parseFloat(result.toString());

    } catch(Exception e) {
        return 0;
    }
}

But it doesn't works, always returns 0. I set the internet permission in AndroidManifest.xml like this:

<uses-permission android:name="android.permission.INTERNET" />

I'm using AVD emulator, but I also tried running the app in my tablet: didn't work.

Any help? Sorry for bad english, I hope you understand.

Julián
  • 101
  • 1
  • 12

2 Answers2

1

Here's the solution:

You can't run this code on the main thread, you have to create another thread like this:

Thread thread = new Thread() {
    @Override
    public void run() {
        String SOAP_ACTION = "http://www.w3schools.com/xml/FahrenheitToCelsius";
        String METHOD_NAME = "FahrenheitToCelsius";
        String NAMESPACE = "http://www.w3schools.com/xml/";
        String URL = "http://www.w3schools.com/xml/tempconvert.asmx";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("Fahrenheit", "10");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        try {
            HttpTransportSE transport = new HttpTransportSE(URL);
            transport.call(SOAP_ACTION, envelope);
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
        } catch (Exception e) {
            Log.e("TESTS", "KSOAP2", e);
        }
    }
};
thread.start();

Anyway, thanks for the answers.

Julián
  • 101
  • 1
  • 12
0
      public static float getCelsius(int Fahrenheit) {
    String Result = null;
    try {
    String SOAP_ACTION = "http://www.w3schools.com/xml/FahrenheitToCelsius";
      String METHOD_NAME = "FahrenheitToCelsius";
     String NAMESPACE = "http://www.w3schools.com/xml/";
      String URL = "http://www.w3schools.com/xml/tempconvert.asmx";
        SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
         request.addProperty("Fahrenheit", Fahrenheit);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
            androidHttpTransport.call(NAMESPACE+ "FahrenheitToCelsius", envelope);
        } catch (IOException | XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SoapPrimitive response;

        try
        {
            response = (SoapPrimitive) envelope.getResponse();
            Result = response.toString();
        }
        catch (Exception e)
        {
        //    Helper.warning("Error","Error 2"+e.toString(), this);
        }
    }
    catch (Exception er) {
      //  Helper.warning("Error","Error 2"+er.toString(), this);
    }
    return Float.parseFloat(Result);
}

Try the above code.

prabhu
  • 89
  • 1
  • 8