android.netandroid-ndk

Not Able To Connect To Webservice With Android


I am not able to connect to webservice with Android - it does not access the webservice code.

I have tried to make thread and then run my code there. I tested the webservice link from my phone and it's reachable.

Here is the code both .NET and:

Webservice code .NET:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
Public Class Service
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As Integer
        Return 0
    End Function

End Class

Here is the code

public class MainActivity extends Activity implements OnClickListener  {
    ImageButton btn1;
    ImageButton Save;

    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
    private static final String MethodName = "HelloWorld";
    private static final String NameSpace = "http://tempuri.org/"; 
    private static final String URL = "http://172.20.10.2/WebSite9/Service.asmx";  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
         final Thread webser = new Thread() {
            public void run() {
                try {
                    // String project = titles.get(position - 1);

                    // CallWebService();
                    SoapObject request = new SoapObject(NameSpace, MethodName);         
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.dotNet=true; 
                    
                    // request.addProperty("TextToDisplay", "This is coming from android");
                    envelope.setOutputSoapObject(request); 

                    androidHttpTransport.call(SOAP_ACTION, envelope); 

                    SoapObject result = (SoapObject)envelope.getResponse(); 
                    //   Toast.makeText(MainActivity.this,result.toString(), Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                  Log.e("tag", e.getMessage());
                }
            }
        };

        webser.start(); 

Solution

  • Your Code Seems to be ok

    please make sure that : -

    1- use ksoap2-android-2.5.2.jar
    2- go to buildpath>add external jars and select it 3- copy the ksoap2-android-2.5.2.jar and paste it under libs file

    restart the application and try ?