user1831490
user1831490

Reputation:

Not connecting to Android KSOAP Webservice

I have a webservice on my localhost which has a simple method to connect to database and also return the name of cities in an array of strings.

Here is the link when I run my service:

http://localhost:1543/Service1.asmx

I'm trying to connect to this webservice via Android application,but I think I don't choose the namespace or soapaction and so forth correctly,So I receive the following exception.

expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @2:7 in java.io.InputStreamReader@41387778)

Here is my android Code:

    public class ProvinceCityActivity extends Activity
{
    EditText txtProvince;
    Button btnResult;

    private static String SOAP_ACTION = "http://tempuri.org/GetCities";

    private static String NAMESPACE = "http://tempuri.org/";
    private static String METHOD_NAME = "GetCities";

    private static String URL = "http://192.168.0.109:1543/Service1.asmx";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtProvince = (EditText) findViewById(R.id.txtProvince);
        btnResult = (Button) findViewById(R.id.btnResult);
        btnResult.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("value", txtProvince.getText().toString());
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;


                try
                {
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);
                    androidHttpTransport.call(SOAP_ACTION, envelope);


                    SoapObject result = (SoapObject) envelope.bodyIn;
                    if (result != null)
                    {
                        String[] strResult = (String[]) result.getProperty(0);
                    Toast.makeText(getApplicationContext(),
                            "Cities: " + strResult[0], 6000).show();
                    }
                } catch (Exception e)
                {
                    Toast.makeText(getApplicationContext(),
                            e.getMessage().toString(), 6000).show();
                }

            }
        });

    }
}

Logcat:

05-15 05:10:07.651: D/dalvikvm(632): GC_CONCURRENT freed 101K, 78% free 462K/2048K, paused 1ms+1ms
05-15 05:10:07.661: D/dalvikvm(632): Debugger has detached; object registry had 1 entries
05-15 05:10:07.671: I/AndroidRuntime(632): NOTE: attach of thread 'Binder Thread #3' failed
05-15 05:10:08.252: D/AndroidRuntime(645): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
05-15 05:10:08.252: D/AndroidRuntime(645): CheckJNI is ON
05-15 05:10:09.132: D/AndroidRuntime(645): Calling main entry com.android.commands.am.Am
05-15 05:10:09.161: I/ActivityManager(73): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=soleimannezhad.android.wCFProvinceCity/.Sohsession13WCFProvinceCityActivity} from pid 645
05-15 05:10:09.171: W/WindowManager(73): Failure taking screenshot for (180x300) to layer 21005
05-15 05:10:09.211: W/NetworkManagementSocketTagger(73): setKernelCountSet(10047, 1) failed with errno -2
05-15 05:10:09.221: D/AndroidRuntime(645): Shutting down VM
05-15 05:10:09.241: D/dalvikvm(645): GC_CONCURRENT freed 102K, 77% free 483K/2048K, paused 2ms+1ms
05-15 05:10:09.241: D/dalvikvm(645): Debugger has detached; object registry had 1 entries
05-15 05:10:09.271: I/AndroidRuntime(645): NOTE: attach of thread 'Binder Thread #3' failed
05-15 05:10:09.751: V/PhoneStatusBar(141): setLightsOn(true)
05-15 05:10:10.191: V/PhoneStatusBar(141): setLightsOn(true)
05-15 05:10:10.321: I/ActivityManager(73): Displayed soleimannezhad.android.wCFProvinceCity/.Sohsession13WCFProvinceCityActivity: +1s115ms
05-15 05:10:10.631: D/dalvikvm(73): GC_CONCURRENT freed 489K, 6% free 12719K/13447K, paused 5ms+54ms
05-15 05:10:10.782: W/NetworkManagementSocketTagger(73): setKernelCountSet(10004, 0) failed with errno -2

Upvotes: 2

Views: 1507

Answers (5)

WALLY
WALLY

Reputation: 11

I'll throw my fix out there... I've run into this several times, and everytime in the past it was the action or the method or the url. However, this time it was the URL in a weird way...

I'm going through some proxy for a current project. For some reason, the URL is case sensitive.

https://abc.gov/webservice/webservice.asmx works

https://abc.gov/WEBSERVICE/webservice.asmx DOES NOT work

Hope this helps some.

Upvotes: 0

user3671455
user3671455

Reputation: 1

package com.example.WSTest;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

//import android.widget.SlidingDrawer;;

public class WSTestActivity extends Activity {

    private static final String SOAP_ACTION = "http://MVIndia.com/AddActivity";
    private static final String METHOD_NAME = "AddActivity";
    private static final String NAMESPACE = "http://MVIndia.com/";
    private static final String URL = "http://10.0.2.2/SAP/Service.asmx";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //final SlidingDrawer txt1 = (SlidingDrawer) findViewById(R.id.Spinner1);
        //final SlidingDrawer txt2 = (SlidingDrawer) findViewById(R.id.Spinner2);
        final EditText txt3 = (EditText) findViewById(R.id.editText3);
        final EditText txt4 = (EditText) findViewById(R.id.editText4);
        final EditText txt5 = (EditText) findViewById(R.id.editText5);
        final EditText txt6 = (EditText) findViewById(R.id.editText6);
        final EditText txt7 = (EditText) findViewById(R.id.editText7);
        final EditText txt8 = (EditText) findViewById(R.id.editText8);
        //final SlidingDrawer txt9 = (SlidingDrawer) findViewById(R.id.Spinner3);
        final EditText txt10 = (EditText) findViewById(R.id.editText10);

        Button button = (Button) findViewById(R.id.btnActivity);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapObject req_AddActivity = new SoapObject(NAMESPACE, METHOD_NAME);
                req_AddActivity.addProperty("strActivity", "");
                req_AddActivity.addProperty("strActivityType", "");
                req_AddActivity.addProperty("strAssignedTo", txt3.getText().toString());
                req_AddActivity.addProperty("strBPCode", txt4.getText().toString());
                req_AddActivity.addProperty("strBPName", txt5.getText().toString());
                req_AddActivity.addProperty("strActivityDate", txt6.getText().toString());
                req_AddActivity.addProperty("strContactPerson", txt7.getText().toString());
                req_AddActivity.addProperty("strEndDate", txt8.getText().toString());
                req_AddActivity.addProperty("strProgress", "");
                req_AddActivity.addProperty("strRemarks", txt10.getText().toString());

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true; 
                envelope.setOutputSoapObject(req_AddActivity);
                HttpTransportSE  androidHttpTransport = new HttpTransportSE (URL);      
                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    Object result = envelope.getResponse();
                    //if (result.toString()=="True")
                    //{
                        //Toast.makeText(WSTestActivity.this, "Activity added Successfully.", Toast.LENGTH_SHORT).show(); 
                    //}
                   Toast.makeText(WSTestActivity.this, result.toString(), Toast.LENGTH_SHORT).show();  
                    //((TextView)findViewById(R.id.lblStatus)).setText(result.toString());
                } catch(Exception E) {
                    Toast.makeText(WSTestActivity.this, E.getClass().getName() + ": " + E.getMessage(), Toast.LENGTH_SHORT).show(); 
                    //((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
                }
                //Toast.makeText(WSTestActivity.this, "Caught", Toast.LENGTH_SHORT).show();  
            }
        });
    }
}

Try this It's working on my local machine. The Webservice returns true if the activity is added successfully. I hope it will help you.

Upvotes: 0

omenocal
omenocal

Reputation: 131

Are you debuggin your app from android Emulator? If so, you should start your URL with "http:// 10.0.2.2" so that the emulator can connect to your local web service...

Upvotes: 2

Lal
Lal

Reputation: 14810

See the image here

enter image description here

SOAP_ACTION : It is your NAMESPACE/METHOD_NAME e.g. "http://ws.android4.com"

METHOD_NAME : It's the name of the Method e.g : "sayHello"

NAMESPACE : It's your NameSapce e.g: "http://ws.android4.com"

For an example webservice see this webservice of w3schools

Here,

METHOD_NAME:CelsiusToFahrenheit

SOAPAction: http://www.w3schools.com/webservices/CelsiusToFahrenheit

NAMESPACE: http://www.w3schools.com/webservices/

Add Internet permission to Androidanifest.xml file.

Upvotes: 2

Aniruddha
Aniruddha

Reputation: 4487

This error comes when you have not provided the correct URL. Please check URL, Method Name and Soap Action of the web service where you have hosted your web service. It is a good practice to use background services like AsyncTask to call webservices.

Upvotes: -1

Related Questions