Can't see output of Web Service on Android device
I have written a Android code that calls a web service.I now want to run that app on my actual phone and not the emulator. When I run the app using Android Emulator and url http://10.0.2.2/test/Service1.asmx , I get to see my output but when I change the url to http://192.168.56.1/test/Service1.asmx , I cannot see the same output on my Android smartphone.It gives an error saying connection refused.
Please can someone help me solve this problem
Here is the android code:
package projects.ksoap2sample;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.*;
import android.os.*;
import android.widget.TextView;
public class ksoap2sam开发者_开发百科ple extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.56.1/test/Service1.asmx";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
call();
}
public void call()
{
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
tv.setText(result.toString());
} catch (Exception e) {
tv.setText(e.getMessage());
}
}
}
This problem doesn't reside with your android source since it is obviously working when the network endpoint is available, rather you problem is most likely due to a network restriction causing the endpoint to not be available.
The most common culprit is a firewall, have you ensured that traffic is allowed in/out on the selected port (assumed 80) from the host of the service?
I believe that you are unaffected when running under the emulator because the way the 10.0.0.2 route works makes the system think as though the traffic originated from the host and is probably exempt from any firewall rules in place that would block remote traffic.
精彩评论