【问题标题】:java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname)java.net.UnknownHostException(无法解析主机“URL”:没有与主机名关联的地址)
【发布时间】:2014-03-14 06:53:07
【问题描述】:

我正在开发一个应用程序,用于在 android.for 中测试 asp.net webservice。因为它使用了一个简单的 webservice 来添加两个数字。数字作为参数传递。结果是在对话中得到。网络服务器在本地工作.当应用程序运行以下结果作为异常时。我在清单中提供了互联网权限。

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(); 
        } });
    }
}

调用者.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();
}

请帮助我。提前谢谢。

【问题讨论】:

  • 我知道这个问题已经得到解答,但是打开我的 Wi-Fi 为我解决了这个问题Exception。我尝试将其发布为答案,但我认为它符合条件的观点已被否决。

标签: android web-services soap


【解决方案1】:

我认为您正在模拟器中对其进行测试。由于该服务位于真实服务器中,因此我更喜欢使用真实设备。另外,使用模拟器是可以的。但是一些模拟器在真实的 Web 服务中表现出奇怪的行为。尝试使用不同的模拟器,最好是新创建的。

【讨论】:

  • 感谢 nizam ..它为我工作..我可以知道原因吗?
  • Appam thinna pore.. :P 由于它是“模拟器”,因此可能会在内部或之前安装的应用程序中出现错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多