I am new to Eclipse and Android programming in general but I have been tasked to create an app for my company that will connect to our SQL Server to read some data as proof of concept. I have downloaded and installed Eclipse Eplison and the Java/Android SDK’s needed. I wanted to start simple so I created an app with a button and a view on it and it builds and works fine. I then created a simple WebService in .NET (vs2010) and have it running on my localhost. Navigating to it works find (via IE) and it works as planned. Here’s the code for the webservice (called Service1.asmx):
[WebMethod]
public string DisplayText(string TextToDisplay)
{
return TextToDisplay;
}
Now I’m trying to make the button call the webservice and display whatever text is passed in. Here’s the code I am using in Eclipse:
package com.test.android;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import android.app.*;
import android.os.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ServiceUI extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://localhost/MyWebService/DisplayText";
private static final String MethodName = "DisplayText";
private static final String NameSpace = "http://tempuri.org/"; // "http://localhost/testservice/";
private static final String URL = "http://localhost/MyWebServices/Service1.asmx?wsdl";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tvMessage);
Button btn = (Button)findViewById(R.id.btnPress);
btn.setOnClickListener(btnListener);
}
public void CallWebService()
{
try {
SoapObject request = new SoapObject(NameSpace, MethodName);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
request.addProperty("TextToDisplay", "This is coming from android");
envelope.setOutputSoapObject(request);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
tv.setText( ""+result);
}
catch (Exception e) {
tv.setText(e.getMessage());
}
}
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
CallWebService();
}
};
}
When I run it in my AVD, I get the following error:
localhost/127.0.0.1:80 – Connection Refused
Any ideas why? I’ve looked at many, many links on here regarding asmx and eclipse but I wasn’t able to find one that referenced my “Connection Refused” error.
Any help is greatly appreciated!
Try the ip address of 10.0.2.2 instead of localhost for your URI.
Check out:
this url
Scroll about half-way down and look for ‘Network Address Space’