我正在开发一个用于在android.for中测试asp.net webservice的应用程序,用于添加两个数字的简单web服务。其中数字作为参数传递。结果进入对话。网络服务器在本地工作。当应用程序运行后,结果get作为exception.i提供了清单中的Internet权限。
java.net.UnknownHostException(无法解析主机 “http://url.com/”:没有与主机名关联的地址)
我的代码在下面给出
主要活动
public static String rslt=""; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
final AlertDialog ad=new AlertDialog.Builder(this).create();
b1.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) {
// TODO Auto-generated method stub
try
{
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
int a=Integer.parseInt(ed1.getText().toString());
int b=Integer.parseInt(ed2.getText().toString());
rslt="START";
Caller c=new Caller();
c.a=a;
c.b=b;
// c.ad=ad;
c.join();
c.start();
while(rslt=="START") {
try {
Thread.sleep(10);
}catch(Exception ex) {
}
}
ad.setTitle("RESULT OF ADD of "+a+" and "+b);
ad.setMessage(rslt);
}catch(Exception ex) {
ad.setTitle("Error!"); ad.setMessage(ex.toString());
}
ad.show();
} });
}
}
Caller.java
public class Caller extends Thread {
public CallSoap cs;
public int a, b;
public void run() {
try {
cs = new CallSoap();
String resp = cs.Call(a, b);
MainActivity.rslt = resp;
} catch (Exception ex) {
MainActivity.rslt = ex.toString();
}
}
}
CallSoap.java
public class CallSoap
{
public final String SOAP_ACTION = "http://tempuri.org/Add";
public final String OPERATION_NAME = "Add";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "url";
public CallSoap()
{
}
public String Call(int a,int b)
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("a");
pi.setValue(a);
pi.setType(Integer.class);
request.addProperty(pi);
pi=new PropertyInfo();
pi.setName("b");
pi.setValue(b);
pi.setType(Integer.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION+OPERATION_NAME, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
请帮助我。谢谢。
答案 0 :(得分:2)
我认为你是在模拟器中测试它。由于服务是在真实服务器中,我更喜欢使用真实设备。此外,使用模拟器是可以的。但是一些模拟器显示了真实Web服务的奇怪行为。尝试使用其他模拟器,最好是新创建的模拟器。