我编写了一个调用Web服务的Android代码。我现在想在我的实际手机而不是模拟器上运行该应用程序。当我使用Android模拟器和网址http://10.0.2.2/test/Service1.asmx运行应用时,我会看到我的输出但是当我将网址更改为http://192.168.56.1/test/Service1.asmx时,我在Android智能手机上看不到相同的输出。它会出错说连接被拒绝了。
请有人帮我解决这个问题
这是android代码:
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 ksoap2sample 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());
}
}
}
答案 0 :(得分:1)
这个问题不在于您的Android源代码,因为当网络端点可用时它显然正常工作,而您的问题很可能是由于网络限制导致端点不可用。
最常见的罪魁祸首是防火墙,您是否确保允许来自服务主机的所选端口(假设为80)进出流量?
我相信您在模拟器下运行时不受影响,因为10.0.0.2路由的工作方式使系统认为流量来自主机,并且可能不受任何阻止远程流量的防火墙规则的影响。