Job
Job

Reputation: 45

Fail to connect android to web service

Hi I am experimenting on android to web service connection. In order to do this, the steps I have done are:

  1. find free web service: http://services.aonaware.com/DictService/DictService.asmx

  2. create a basic activity on Android Studio

  3. change the Manifest file to have permission to use internet by adding the following line:

    uses-permission android:name="android.permission.INTERNET"

  4. add the ksoap2 library

  5. Try to consume the web service using the following code:

declarations

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

    tv = (TextView) findViewById(R.id.textView1);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    PropertyInfo property = new PropertyInfo();
    property.setName("word");
    //property.setType(String.class);
    property.setValue("computer");
    request.addProperty(property);


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

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    Object response = null;
    try {
        androidHttpTransport.getServiceConnection();

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        tv.setText("success");
        response =  envelope.getResponse();
        response.toString();
        String resultData= response.toString();
        tv.setText(resultData);

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

However, textView1 is not getting the result. Is there anything wrong with my code?

Note: I use Android Studio with Gradle version 2.10

Upvotes: 1

Views: 1188

Answers (2)

Techidiot
Techidiot

Reputation: 1947

Try using AsyncTask for executing Webservices. Here's a sample -> android.os.NetworkOnMainThreadException for webservice (ksoap)

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    private String TAG ="Vik";

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

        AsyncCallWS task = new AsyncCallWS();
        task.execute(); 
    }

    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            Log.i(TAG, "doInBackground");
            calculate();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

    public void calculate() 
    {
        String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
        String METHOD_NAME = "CelsiusToFahrenheit";
        String NAMESPACE = "http://tempuri.org/";
        String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("Celsius", "32");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

            Log.i(TAG, "Result Celsius: " + resultString);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }

        SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
        METHOD_NAME = "FahrenheitToCelsius";
        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("Fahrenheit", "100");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

            Log.i(TAG, "Result Fahrenheit: " + resultString);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }
    }

}

Upvotes: 1

Kristy Welsh
Kristy Welsh

Reputation: 8530

Here is a routine for a ASP.Net web service:

public static SoapPrimitive callProcServiceForObject(String serviceMethod, String storedProc, String params) throws Exception {
    String NAMESPACE = "http://YourWebServiceURL.com";
    String METHOD_NAME = getMethodName(serviceMethod);
    String SOAP_ACTION = NAMESPACE + METHOD_NAME;
    String URL = "http://YourWebServiceURL.com";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("sPassword", YourPassword);
    request.addProperty("sData", GlobalVars.serverDB);
    request.addProperty("sSP_Name", storedProc);
    request.addProperty("sParam", params);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    // Enable the below property if consuming .Net service
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);

    SoapObject returnable = null;
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        returnable = (SoapObject)envelope.getResponse();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Msg:" + e.getMessage() + "; SP:" + storedProc + "; Params: " + params + "; Method:" + METHOD_NAME);
    }
    return returnable;
}

Reading the SOAP object:

SoapPrimitive response = callProcServiceForObject(storedProcedureName, params);
if (response != null) {
    String sResponse = response.toString();
}

Upvotes: 0

Related Questions