我正在android中开发一个应用程序,它连接到wcf服务并返回结果。wcf是用soap编写的,我使用KSOAP2进行连接。
起初我连接到某个网址,一切都运行良好。然后我必须更改 url,并收到未知异常 无法解析主机:没有与名称关联的地址
。
我已经完成了研究,所有其他类似问题的解决方案是在 Android Manifest 互联网连接中添加。但我已经这样做了。有什么建议吗?另一个细节是,使用第一个网址(我的应用程序在其中运行良好)是一个正常网址,第二个网址位于本地服务器中,顺便说一句,尽管我的电脑在该服务器中正确连接。(正确的意思是,当我使用我的浏览器的 url 连接没有问题)
我在这里发布建立连接的类:
公共(public)类 KSoapConnector 实现 InstantiateConnection{
private SoapObject response;
private WcfReceivedClass identifier;
private WcfReceivedClass[] receivedData;
private final String NAMESPACE = "http://microsoft.com/webservices/";
private String METHOD_NAME="GetTeacherClass";
private String SOAP_ACTION = "http://microsoft.com/webservices/IVertiSchool_SRV/GetTeacherClass";
private final String URL = "http://vertiserv1:81/VertiSchool_SRV.svc";
//below is the url which was working fine
//private final String URL = "http://wcf.schoolportal.gr/VertiSchool_SRV.SVC";
public KSoapConnector(String methodName,int mode){
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
//here are the parameters for the wcf service and there values are correctly put
Request.addProperty("connString", "connectionString");
Request.addProperty("TeCode", "tCode");
Request.addProperty("Company", "cmp");
Request.addProperty("Lng", "lng");
Request.addProperty("MainPeriod", "mainPrd");
SoapSerializationEnvelope envelope = new SerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.addMapping(NAMESPACE, "SRV_Class",new SRV_Class().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
//here is where i get the exception...
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapObject)envelope.getResponse();
receivedData = identifier.retrieveFromSoap(response);
}
catch(Exception e){
e.printStackTrace();
}
}
这是我的 list 文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListOfClasses"></activity>
</application>
我知道在 Android 中本地主机是 10.0.2.2,但我的应用程序直接连接到本地服务器...我应该转发任何端口吗?
最佳答案
您的 SOAP_ACTION 应该是这样的。
AppConsts.NAMESPACE = "http://www.tempuri.us/"
usecaseString = THIS SHOULD BE YOUR METHOD NAME OF SERVICE.
String soapAction = AppConsts.NAMESPACE + usecaseString;
你的 URL 应该是这样的。
NAMESPACE+/PROJECTNAME/SERVICE_NAME.asmx
关于java - Android应用程序无法解析主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11499938/