java.net.UnknownHostException:url当我在线提供服务时出现异常

时间:2013-09-03 11:44:18

标签: android android-emulator

我有通过asp.net webservice进行sqlserver连接的代码。

当服务在本地时,代码运行良好。

但是开始给我错误:

  

的java.net.UnknownHostException:URL

因为我在网上提供服务。

我尝试添加以下内容以显示:

 <uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

还删除了旧模拟器并创建了新模拟器。

还重启eclips,我的电脑。

但仍然是错误。

CODE:

public class CallertoService {

public final String SOAP_ACTION1 = "http://tempuri.org/ValidateUser";
public final String SOAP_ACTION2 = "http://tempuri.org/GetMessages";
public final String SOAP_ACTION3 = "http://tempuri.org/Register";
public final String SOAP_ACTION4 = "http://tempuri.org/UpdateMessages";

public final String OPERATION_NAME1 = "ValidateUser";
public final String OPERATION_NAME2 = "GetMessages";
public final String OPERATION_NAME3 = "Register";
public final String OPERATION_NAME4 = "UpdateMessages"; 

public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

public  final String SOAP_ADDRESS = "http://www.graficali.co.in/android/service.asmx";



public CallertoService(){}

public String Register(String FullName,String UserName,String Email,String gender,String DateOfBirth,String Password){

    SoapObject req=new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME3);

    PropertyInfo pi=new PropertyInfo();
    pi.setName("FullName");
    pi.setValue(FullName);
    pi.setType(String.class);
    req.addProperty(pi);

    pi=new PropertyInfo();
    pi.setName("UserName");
    pi.setValue(UserName);
    pi.setType(String.class);
    req.addProperty(pi);


    pi=new PropertyInfo();
    pi.setName("Email");
    pi.setValue(Email);
    pi.setType(String.class);
    req.addProperty(pi);


    pi=new PropertyInfo();
    pi.setName("Gender");
    pi.setValue(gender);
    pi.setType(String.class);
    req.addProperty(pi);

    pi=new PropertyInfo();
    pi.setName("DateOfBirth");
    pi.setValue(DateOfBirth);
    pi.setType(String.class);
    req.addProperty(pi);

    pi=new PropertyInfo();
    pi.setName("Password");
    pi.setValue(Password);
    pi.setType(String.class);
    req.addProperty(pi);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    envelope.dotNet = true;

    envelope.encodingStyle = SoapEnvelope.ENC;
    envelope.setAddAdornments(false);
    envelope.implicitTypes = false;
    envelope.setOutputSoapObject(req);


     try {
          InetAddress i = InetAddress.getByName(SOAP_ADDRESS);
        } catch (UnknownHostException e1) {
          e1.printStackTrace();
        }

    HttpTransportSE  httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response=null;

     try
     {
         httpTransport.call(SOAP_ACTION3, envelope);
         response = envelope.getResponse();
     }
     catch (Exception exception)
     {
         response=exception.toString();
     }
     return  response.toString();

}

以下是mylogcat:

09-03 17:03:41.463: W/System.err(798): java.net.UnknownHostException: http://www.graficali.co.in/android/service.asmx
09-03 17:03:41.483: W/System.err(798):  at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
09-03 17:03:41.483: W/System.err(798):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
09-03 17:03:41.493: W/System.err(798):  at java.net.InetAddress.getByName(InetAddress.java:325)
09-03 17:03:41.493: W/System.err(798):  at com.example.messagereader.CallertoService.Register(CallertoService.java:86)
09-03 17:03:41.493: W/System.err(798):  at com.example.messagereader.Register$1.onClick(Register.java:67)
09-03 17:03:41.503: W/System.err(798):  at android.view.View.performClick(View.java:2485)
09-03 17:03:41.503: W/System.err(798):  at android.view.View$PerformClick.run(View.java:9080)
09-03 17:03:41.503: W/System.err(798):  at android.os.Handler.handleCallback(Handler.java:587)
09-03 17:03:41.513: W/System.err(798):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 17:03:41.513: W/System.err(798):  at android.os.Looper.loop(Looper.java:123)
09-03 17:03:41.523: W/System.err(798):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-03 17:03:41.523: W/System.err(798):  at java.lang.reflect.Method.invokeNative(Native Method)
09-03 17:03:41.523: W/System.err(798):  at java.lang.reflect.Method.invoke(Method.java:507)
09-03 17:03:41.533: W/System.err(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-03 17:03:41.533: W/System.err(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-03 17:03:41.533: W/System.err(798):  at dalvik.system.NativeStart.main(Native Method)
09-03 17:05:03.548: W/jdwp(798): Debugger is telling the VM to exit with code=1
09-03 17:05:03.548: I/dalvikvm(798): GC lifetime allocation: 2605 bytes
09-03 17:05:03.583: D/Zygote(32): Process 798 exited cleanly (1)
09-03 17:05:03.594: I/ActivityManager(76): Process com.example.messagereader (pid 798) has died.
09-03 17:05:03.602: E/InputDispatcher(76): channel '4082bc98 com.example.messagereader/com.example.messagereader.Login (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
09-03 17:05:03.602: E/InputDispatcher(76): channel '4082bc98 com.example.messagereader/com.example.messagereader.Login (server)' ~ Channel is unrecoverably broken and will be disposed!
09-03 17:05:03.612: I/WindowManager(76): WIN DEATH: Window{4082bc98 com.example.messagereader/com.example.messagereader.Login paused=false}
09-03 17:05:03.622: I/WindowManager(76): WIN DEATH: Window{406b0bc8 com.example.messagereader/com.example.messagereader.Register paused=false}
09-03 17:05:03.632: E/InputDispatcher(76): Received spurious receive callback for unknown input channel.  fd=166, events=0x8
09-03 17:05:03.712: I/ActivityManager(76): Start proc com.example.messagereader for activity com.example.messagereader/.Login: pid=808 uid=10030 gids={3003}
09-03 17:05:04.322: D/dalvikvm(808): GC_EXTERNAL_ALLOC freed 45K, 53% free 2547K/5379K, external 2002K/2137K, paused 48ms
09-03 17:05:04.632: W/InputManagerService(76): Got RemoteException sending setActive(false) notification to pid 798 uid 10030
09-03 17:05:04.762: I/ActivityManager(76): Displayed com.example.messagereader/.Login: +1s82ms
09-03 17:05:05.312: D/dalvikvm(160): GC_EXTERNAL_ALLOC freed 50K, 50% free 3482K/6855K, external 6963K/7282K, paused 514ms

请帮帮我。

2 个答案:

答案 0 :(得分:1)

尝试PING您的服务器。确保您的服务器reachable正常工作。

更新: 如果您遇到Android模拟器的问题,那么您可以尝试新的Android模拟器替代Genymotion

答案 1 :(得分:1)

此错误发生在仿真器中,无论是通用仿真器还是Genymotion。修复就像确保这些一样简单。

  
      
  1. 您的应用是否在Android Manifest的正确位置拥有INTERNET权限?
  2.   
  3. URI真的有效吗?从浏览器中查找。
  4.   
  5. 仿真器是否具有网络连接?它是否在顶级状态栏显示Wifi /数据?如果在可用时未在“设置”中启用它。在   Genymotion就像连接到WiFi中的有线SSID一样简单   设置。
  6.   

总是更好地检查网络连接并抛出消息/吐司,所以我们不要加扰。

public static boolean isNetworkAvailable(Context context) {
    if (context != null) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    } else
        return true;
}

这需要Android权限ACCESS_NETWORK_STATE。