gpt4 book ai didi

java - Android - 当我在设备上运行它时无法访问网络服务,但使用 AVD 它可以正常工作。为什么?

转载 作者:行者123 更新时间:2023-11-28 22:30:31 27 4
gpt4 key购买 nike

我有这个位置的网络服务:

<wsdlsoap:address location="http://app.example.com:8080/WSTest/services/Hello"/>

我想通过我的 Android 应用程序访问此网络服务。我这样做:

private class ConnectTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {

private final String NAMESPACE = "http://ws.audiomobil.com";
private final String URL = "http://app.example.com:8080/WSTest/services/Hello?wsdl";
private final String SOAP_ACTION = "http://ws.audiomobil.com/hello";
private final String METHOD_NAME = "hello";

try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

String firstName = "Android";
String lastName = "Program";
/*
// Pass value for fname variable of the web service
PropertyInfo fnameProp = new PropertyInfo();
fnameProp.setName("fname"); // Define the variable name in the web service method
fnameProp.setValue(firstName); // Define value for fname variable
fnameProp.setType(String.class); // Define the type of thevariable
request.addProperty(fnameProp); // Pass properties to thevariable

// Pass value for lname variable of the web service
PropertyInfo lnameProp = new PropertyInfo();
lnameProp.setName("lname");
lnameProp.setValue(lastName);
lnameProp.setType(String.class);
request.addProperty(lnameProp);
*/
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);

try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String s = response.toString();
System.out.println("WS: " + s);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return null;

}
}

++++++编辑我的网络服务类是这样的:

public class Hello {
public String hello(String fname, String lname){
return "Hello";
}
}

++++++++

当我在我的 AVD 模拟器 Android 4.0.3 上运行它时,它工作正常。它可以访问网络服务,我得到了响应。

但是当我在我的设备版本 4.0.3 上运行它时,它无法正常工作。它在此时停止:androidHttpTransport.call(SOAP_ACTION, envelope);

它会抛出一个 XMLPullParserException。这是 logcat:

    01-23 11:24:36.959: I/System.out(8172): org.xmlpull.v1.XmlPullParserException: 

unterminated entity ref (position:TEXT @1:86 in java.io.InputStreamReader@4188c258)

我真的不知道这是怎么发生的。谁能帮帮我?

最佳答案

我认为您缺少 xml 版本标记。调用前尝试设置xml版本标签

androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);

示例代码:

public static String webServiceForScriptTimeLimit() {
String scriptTimeLimitResponse = null;
try {
SoapObject request = new SoapObject(NAMESPACE,
SCRIPT_TIMELIMIT_METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
HttpTransportSE ht = new HttpTransportSE(SOAP_ADDRESS);
ht.debug = true;
ht.call(SOAP_ACTION_TIME_LIMIT, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();
scriptTimeLimitResponse = response.toString();

}

catch (Exception ex) {
FileLog.logInfo(
"Exception ---> WS_ERROR ---> WebServiceUtility: webServiceForScriptTimeLimit() - "
+ ex.toString(), 0);
}
return scriptTimeLimitResponse;
}

关于java - Android - 当我在设备上运行它时无法访问网络服务,但使用 AVD 它可以正常工作。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21305482/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com