We help IT Professionals succeed at work.
Get Started

Call WebService from Android

95 Views
Last Modified: 2020-03-31
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"
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE