1

i created a sample android app i.e. calling simple .net webservice and displaying in TextView, below are the code prospects:

web service code:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string MobileApp(string acNum) 
    {
        return "Account number is"+acNum;
    }

}

android app code:

import java.io.IOException;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;


public class AccountDetails extends TabActivity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/MobileApp";

    private static final String METHOD_NAME = "MobileApp";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://localhost:58817/MobService/Service.asmx";
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accountdet);
        tv=(TextView)findViewById(R.id.tView);
ServiceCall();
    }
    public void ServiceCall()
    {
        try{
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        request.addProperty("acNum", "123456");

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


        androidHttpTransport.call(SOAP_ACTION, envelope);

        SoapObject result = (SoapObject)envelope.getResponse();
        tv.setText(result.toString());

        }       
        catch (final IOException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (final XmlPullParserException e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (final Exception e)

        {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }


    }

Also i added below lines to the manifest file:

<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-sdk android:minSdkVersion="7" />

But i didn't get the result in TextView, so any one plz help me.

@nagaraju.

13
  • Do you know whether your web service receives the call?
    – Shlublu
    Commented Jul 29, 2011 at 11:13
  • localhost is android-emulator not your PC
    – Selvin
    Commented Jul 29, 2011 at 11:16
  • @shlublu how can i check it ?
    – nag
    Commented Jul 29, 2011 at 11:19
  • @selvin Then is there any changes i've to do?
    – nag
    Commented Jul 29, 2011 at 11:20
  • 1
    just change localhost to 10.0.2.2 or to your.computer.local.ip
    – Selvin
    Commented Jul 29, 2011 at 11:22

2 Answers 2

1

First check whether your service is running-- for that try in the browser with your link,link,then create dummy site , in there call your method MobileApp, If its return then your service is working prefect.

  SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap//// envelope
    envelopes.dotNet = true;
    envelopes.setOutputSoapObject(requestData);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(APPURL);
    httpTransport.debug = true;

    try {
        httpTransport.call(SOAP_ACTION1, envelopes);
        responsesData = (SoapPrimitive) envelopes.getResponse();

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

Try This one..

17
  • Hi Nagkeeran, Thanx for your reply currently i'm using HttpTransportSE class for transmitting the msg is this correct one? if i use AndroidHttpTransport class it showing warning like "The type AndroidHttpTransport is deprecated", Still i didn't getting the result after changing code as you said that :(
    – nag
    Commented Aug 1, 2011 at 6:08
  • your tesing/ dummy site return (service) any result? did you try?
    – Piraba
    Commented Aug 1, 2011 at 6:13
  • Actually i'm running my service in other local system from there i'm using that service, but if i paste that local system address in my browser it doesn't displaying the service. So is there any changes i've to when i'm using service from local system 'm giving link like "192.158.0.34:54888/MobService/Service.asmx"
    – nag
    Commented Aug 1, 2011 at 6:18
  • So Problem in your service.. If the service running, that mean showing xml format result in your browser then only you can make sure your service is working
    – Piraba
    Commented Aug 1, 2011 at 6:23
  • ya that's ok. But even i'm running service in my system not other one service is running fine and when 'm clicking invoke button it showing me XML format but didn't getting response. The link which 'm using in my system is like "192.188.0.77:52289/MobService/Service.asmx" this link is using in my code.
    – nag
    Commented Aug 1, 2011 at 6:27
0

I found my bug, it is simple, As i put user permission lines in manifest file in the application tag but actually, I've to put outside of the application tag so that's why i'm getting the response.

Thanks one n all for replying me, your replies also help me to learn stuff :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.