Link to home
Start Free TrialLog in
Avatar of Neha Ninave
Neha Ninave

asked on

Call WebService from Android

I am just a beginner in Android. I have to call a web service which I have develop in .NET from Android Studio.  I found lots of code from google but still unable to figure out an issue
Here is my code I don't know if it's correct
public class MainActivity extends AppCompatActivity {

    EditText LoginText;

    String SOAP_ACTION = "https://mywebsite.com/ReadLogin";
    String METHOD_NAME = "ReadLogin";
    String namespace = "https://mywebsite.com/";
    String url = "https://mywebsite/login.asmx";

public void checkLogin(View view) {
        String login = LoginText.getText().toString();
        RequestParams params = new RequestParams();
        if (isNotNull(login)) {
            //params.put("username", login);
            call();
            //invokeWS(params);
        }
        else {
            Toast.makeText(getApplicationContext(), "Please enter valid email", Toast.LENGTH_LONG).show();
        }
    }


public void call()
    {
        try {

            SoapObject request = new SoapObject(namespace, METHOD_NAME);

            request.addProperty("username", "abc");

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
            androidHttpTransport.debug = true;

            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;


            //final Vector<SoapObject> response = (Vector<SoapObject>)envelope.getResponse();
            //final String str = response.toString();

            //Object result = (Object)envelope.getResponse();
            //String myResult = result.toString();

            Toast.makeText(getApplicationContext(), resultsRequestSOAP.toString(), Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            String mError =e.toString();
            Toast.makeText(getApplicationContext(), mError, Toast.LENGTH_LONG).show();
        }
    }

Open in new window


In catch exception I am getting an error like
android.os.NetworkOnMainThreadException

In AndroidManifest.xml I already added
<uses-permission android:name="android.permission.INTERNET" />
and
android:usesCleartextTraffic="true"
ASKER CERTIFIED SOLUTION
Avatar of Neha Ninave
Neha Ninave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial