Search code examples
androidasp.neteclipseexceptionandroid-ksoap2

Exception using Ksoap in android


Hello I am trying to solve this problem for past 5 hours now my web service use to work fine until I recently installed windows 8 and setup eclipse and android SDK.

My problem is that I added external jar ksoap and created a sample web service for the test.

When calling for the Soap response exception is created
"11-01 11:10:00.161: E/dalvikvm(304): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.example.testweb.MainActivity.Send"

Below is my xml code for the mainActvity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

   <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="194dp"
    android:text="TextView" />

  </RelativeLayout>

MainActivity.class

package com.example.testweb;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {


 private static final String NAMESPACE = "http://tempuri.org/";
 private static final String URL = "http://10.0.2.2:34253/Service1.asmx"; 
 private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
 private static final String METHOD_NAME = "HelloWorld";




   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Send();
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }




   public void Send()
   {
     SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

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

       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 5000);

       try
       {
            androidHttpTransport.call(SOAP_ACTION, envelope);
           // SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
            //Hello World; }
            String result = resultsRequestSOAP.toString();
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();

       }
       catch (Exception e) {
        // TODO: handle exception
           Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }

}

}

AndroidMenifest.XML

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.testweb"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

ScreenShot of the Ksoap.jar Ksoap Reference
enter image description here

My ASP.NET 3.5 Webservice code which i am running on localhost

/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
 }

I am using google api 8 and my ADT is 20.0.3 and my ksoap.jar is 2.6.5

Edit Guys my Jar problem is solved now a new error i am receiving which occurs on this link

    androidHttpTransport.call(SOAP_ACTION, envelope);

And the exception in the try catch gives me this error

     org.xmlpull.v1.XmlPullParserException: expected: START_TAG {schemas.xmlsoap.org/soap/envelope}Envelope (position:START_TAG <HTML>@2:7 in     java.io.InputStreamReader@44ef85f0)

Thanks in advance


Solution

  • In the order and export setting try moving KSOP lib to top of the list.

    EDIT 1: It looks like for some reason android not been able to connect to your sever and ksoap2 is not able to handle http response codes properly by default. it tries to parse a http error message like "file not found" and throws an exception as it is not valid SOAP. Try to use the ksoap2 source code in place of lib and check the http response code. source code is available at the this link..