2

I am a newbie in android development and struggling to create an android client which can consume my wcf service. below is my wcf service interface. I am returning a hardcoded values for both the functions. WCF service is using BasicHttpBinding.

[ServiceContract]
  public interface IAgent
    {

        [OperationContract]
        int TestMethod();

        [OperationContract]       
        string TestMethodParameter(int key);
    }

below is the android client code

package com.example.test;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.view.Menu;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity
extends Activity 
implements OnClickListener
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
     private static final String TEST_METHOD = "IAgent/TestMethod";
     private static final String NAMESPACE = "http://tempuri.org/";
     private static final String URL = "http://odchistorian.cloudapp.net/HistorianAgent.svc";
     private static final String SOAP_ACTION1 = NAMESPACE + TEST_METHOD;
    Button connectButton = null;
    EditText editBox = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connectButton = (Button) this.findViewById(R.id.button1);
        editBox = (EditText) this.findViewById(R.id.editText1);
        connectButton.setOnClickListener(this);
        StrictMode.setThreadPolicy(policy); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {

        SoapObject request = new SoapObject(NAMESPACE,TEST_METHOD); 
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        final HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);              
                    try {

                        androidHttpTransport.call(SOAP_ACTION1, envelope);
                        SoapPrimitive response = null;
                            response = (SoapPrimitive) envelope.getResponse();
                            String resultData= response.toString();              
                            editBox.setText(resultData);

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                           
    }
}

Stack Trace (LogCat)

12-07 17:19:20.244: W/System.err(1180): java.lang.IllegalArgumentException: size <= 0
12-07 17:19:20.261: W/System.err(1180):     at java.io.BufferedInputStream.<init>(BufferedInputStream.java:94)
12-07 17:19:20.271: W/System.err(1180):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:226)
12-07 17:19:20.271: W/System.err(1180):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
12-07 17:19:20.281: W/System.err(1180):     at com.example.test.MainActivity.onClick(MainActivity.java:103)
12-07 17:19:20.281: W/System.err(1180):     at android.view.View.performClick(View.java:4202)
12-07 17:19:20.291: W/System.err(1180):     at android.view.View$PerformClick.run(View.java:17340)
12-07 17:19:20.291: W/System.err(1180):     at android.os.Handler.handleCallback(Handler.java:725)
12-07 17:19:20.291: W/System.err(1180):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 17:19:20.301: W/System.err(1180):     at android.os.Looper.loop(Looper.java:137)
12-07 17:19:20.301: W/System.err(1180):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-07 17:19:20.311: W/System.err(1180):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 17:19:20.311: W/System.err(1180):     at java.lang.reflect.Method.invoke(Method.java:511)
12-07 17:19:20.321: W/System.err(1180):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-07 17:19:20.321: W/System.err(1180):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-07 17:19:20.331: W/System.err(1180):     at dalvik.system.NativeStart.main(Native Method)

Every time run this code i always get IllegalArgumentException on line "androidHttpTransport.call(SOAP_ACTION1, envelope);". I have gone through several blogs, but no luck. Could't get what exactly is missing. Please help.

harshbodhi
  • 99
  • 9
  • http://stackoverflow.com/questions/13412622/ksoap2-java-lang-illegalargumentexception-size-0-while-making-a-http-call It must be the server returning an empty response. You can use the Fiddler tool to track the traffic and see what's wrong. – Jong Dec 07 '12 at 20:32
  • I used the fiddler and inspected the response header.It is 400 Bad Request and Content-Length is 0. Does it means that call is not reaching to service or method request is not correct? – harshbodhi Dec 08 '12 at 06:42
  • It means the request isn't correct. It might be a wrong `Content-type`: http://stackoverflow.com/a/13484685/797390 – Jong Dec 08 '12 at 11:45
  • How can I set the content-type? – harshbodhi Dec 12 '12 at 07:19

0 Answers0