Arif YILMAZ
Arif YILMAZ

Reputation: 5866

android ksoap2 crashes and I cannot catch it

I am following this tutorial . it is very straight-forward and I like it but It crashes on emulator. I cannot catch the error. I dont understand the problem. here is the piece of code.

private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
private final String METHOD_NAME = "ConvertWeight";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    String weight = "3700";
    String fromUnit = "Grams";
    String toUnit = "Kilograms";


    PropertyInfo weightProp =new PropertyInfo();
    weightProp.setName("Weight");
    weightProp.setValue(weight);
    weightProp.setType(double.class);
    request.addProperty(weightProp);

    PropertyInfo fromProp =new PropertyInfo();
    fromProp.setName("FromUnit");
    fromProp.setValue(fromUnit);
    fromProp.setType(String.class);
    request.addProperty(fromProp);

    PropertyInfo toProp =new PropertyInfo();
    toProp.setName("ToUnit");
    toProp.setValue(toUnit);
    toProp.setType(String.class);
    request.addProperty(toProp);

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

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        Log.i("myApp", response.toString());

        TextView tv = new TextView(this);
        tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
        setContentView(tv);

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

}

what do you think the problem is? I downloaded ksoap2 library and included in the project. I tried it on my real device too and it works. I also enabled the internet access on config file.

any help will be great. or can you suggest another tutorial?

Upvotes: 0

Views: 182

Answers (1)

Chathura Priyankara
Chathura Priyankara

Reputation: 73

After Android 3.0 you cannot access web in activity's main thread, you need to start new thread to access the web

Upvotes: 1

Related Questions