0

我有通过 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,我的电脑。

但仍然是错误。

代码:

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

请帮我。

4

2 回答 2

1

试试PING你的服务器。确保您的服务器正在reachable运行。

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

于 2013-09-03T11:53:09.727 回答
1

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

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

总是更好地检查网络连接并抛出消息/敬酒,所以我们不会争吵。

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。

于 2015-07-25T20:07:49.463 回答