views:

57

answers:

2

Hi I am trying to login salesforce through a webservice on Android platform. But I am facing some problems. I am getting a "Socket Exception:Operation timed out" error. I am using the Ksoap2 soap cient ported for Android and WSDL details are from the Partner WSDL of my developer account at Salesforce.I have anabled the "api access".Also, username and password are valid. I am trying this through development enviornment(Eclipse) on my desktop emulator for Android.
Can anyone please help me figure out if I am missing something and resolve this problem.
Following is the source code:

import android.app.Activity;
//Soap imports
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

//Android Imports
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.xmlpull.v1.*;
import java.io.IOException;


import android.os.Bundle;

public class SalesForceLogin extends Activity 
{
    private static final String NAMESPACE = "urn:sobject.partner.soap.sforce.com";
    private static final String URL = "https://login.salesforce.com/services/Soap/c/20.0";
    private static final String SOAP_ACTION = "";
    private static final String TAG = "SALESFORCE-SERVICE";

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

        //New
        SoapObject binding = new SoapObject(NAMESPACE, "login");
        binding.addProperty("username", "username"); //Valid username
        binding.addProperty("password", "password"); //Valid password appended by security token
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(binding);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        androidHttpTransport.debug = true; 
        SoapObject result=null;
        try 
        {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            result = (SoapObject)envelope.getResponse();
            // At this point, Soap login success.
            Log.v(TAG, result.toString());
            Object serverUrl=result.getProperty("serverUrl");
            Log.v(TAG,"serverUrl="+serverUrl);
            Object passwordExpired=result.getProperty("passwordExpired");
            Log.v(TAG,"passwordExpired="+passwordExpired); 
            Log.v(TAG, "Ending Login with Success.");

        } 
        catch (SoapFault aSoapFault) 
        {
            Log.d(TAG, "SoapFault actor="+aSoapFault.faultactor);
            Log.d(TAG, "SoapFault code="+aSoapFault.faultcode);
            Log.d(TAG, "SoapFault message="+aSoapFault.faultstring);
            // do something – Soap Fault occurred
        }
        catch (XmlPullParserException reso)
        {
            Log.v(TAG, "XmlPullParserException");
            reso.printStackTrace();
        }
        catch (IOException reso) //Socket Exception caught here!
        {
            Log.v(TAG, "IOException");
            reso.printStackTrace();
        }

    }
}

Edit: For reference of anyone who refers this thread, I was able to get above web service to run, problem was discrepancy between url and namespace. With following modifications the above code runs perfectly fine.

NAMESPACE = "urn:partner.soap.sforce.com";
URL = "https://login.salesforce.com/services/Soap/u/20.0";
SOAP_ACTION = "soap";
+1  A: 

I have some experience using Ksoap2-Android for .NET webservices, and the first thing i can tell you is that they are difficult at best. I pray that Google will make a native soap client in the future...

As for your problem, its difficult to say what could be wrong without knowing details about your web service. A SocketException can be thrown during socket creation or setting options (see google api). Chances are you are using the wrong URL or namespace.

I would also suggest is using a packet sniffer (wireshark is good for this) so you can see what your soap requests actually look like going out. You can then compare this to what the service is expecting and fine tune it from there.

Good luck! let me know if i can help more

mtmurdock
@mtmurdock: Thanks for that quick info, I will check out the traffic with wireshark. I was just browsing about the problem and also found, that system proxy settings do not apply to plain sockets, and I am indeed using a proxy, so will first will check without a proxy, I am new to Webservices so maybe i might have misread the WSDL for parameters perhaps i will post the WSDL relevant sections if you can help.
Als
+1  A: 

Routing issues maybe? (ie. port forwarding, proxies, firewalls, etc.)

Vuk
@Vuk: Exactly my feeling right now. I am going to try out first on a proxyless desktop..will update the results.
Als
Yeah, those routing thingies do tend to be a pain in the neck and huuuge time-wasters.
Vuk