gpt4 book ai didi

java - 检索 WebService 数组 Java(Android)

转载 作者:行者123 更新时间:2023-11-30 03:48:22 26 4
gpt4 key购买 nike

我必须检查用户是否输入了正确的代码才能检索这些信息。请帮忙!下面的代码是我的 SOAP 的样子:

<retrieveAudioInfoResponse xmlns="http://tempuri.org/">
<retrieveInfoResult>
<Info>
<date>string</date>
<name>string</name>
<id>int</id>
<description>string</description>
<url>string</url>
</Info>

这是我为我的类(class)从网络服务获取数据所做的:

private void retrieveInfo(String code){

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("code", code);


SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
System.out.println(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;

try {

androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
infoList = new String[response.getPropertyCount()];

for(int i=0;i<response.getPropertyCount();i++){

infoList[i] = response.getProperty(i).toString();
Log.d("This is the response",infoList[i]);
}
}
catch (Exception e) {
e.printStackTrace();
}
}

我的网络服务:

POST /mobile/.../....asmx HTTP/1.1
Host: ...
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/retrieveInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<retrieveInfo xmlns="http://tempuri.org/">
<code>string</code>
</retrieveInfo>
</soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<retrieveInfoResponse xmlns="http://tempuri.org/">
<retrieveInfoResult>
<Info>
<date>string</date>
<name>string</name>
<id>int</id>
<description>string</description>
<url>string</url>
</Info>
<Info>
<date>string</date>
<name>string</name>
<id>int</id>
<description>string</description>
<url>string</url>
</Info>
</retrieveInfoResult>
</retrieveInfoResponse>
</soap:Body>
</soap:Envelope>

我收到的错误是:

01-31 13:15:00.708: D/dalvikvm(13583): GC_FOR_ALLOC freed 85K, 3% free 13108K/13447K, paused 12ms
01-31 13:15:00.713: I/dalvikvm-heap(13583): Grow heap (frag case) to 15.312MB for 2457616-byte allocation
01-31 13:15:00.738: D/dalvikvm(13583): GC_CONCURRENT freed 1K, 3% free 15507K/15879K, paused 2ms+2ms
01-31 13:15:00.788: D/dalvikvm(13583): GC_FOR_ALLOC freed 0K, 3% free 15507K/15879K, paused 13ms
01-31 13:15:00.798: I/dalvikvm-heap(13583): Grow heap (frag case) to 19.476MB for 4367376-byte allocation
01-31 13:15:00.823: D/dalvikvm(13583): GC_CONCURRENT freed 0K, 2% free 19772K/20167K, paused 2ms+2ms
01-31 13:15:00.898: W/System.err(13583): android.os.NetworkOnMainThreadException
01-31 13:15:00.898: W/System.err(13583): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-31 13:15:00.898: W/System.err(13583): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-31 13:15:00.898: W/System.err(13583): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-31 13:15:00.903: W/System.err(13583): at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-31 13:15:00.903: W/System.err(13583): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-31 13:15:00.903: W/System.err(13583): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:75)
01-31 13:15:00.903: W/System.err(13583): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:136)
01-31 13:15:00.903: W/System.err(13583): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:90)
01-31 13:15:00.903: W/System.err(13583): at com.example.loginws.second.WebServiceCallExample(second.java:80)
01-31 13:15:00.903: W/System.err(13583): at com.example.loginws.second.onCreate(second.java:32)
01-31 13:15:00.908: W/System.err(13583): at android.app.Activity.performCreate(Activity.java:4470)
01-31 13:15:00.908: W/System.err(13583): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-31 13:15:00.908: W/System.err(13583): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-31 13:15:00.908: W/System.err(13583): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-31 13:15:00.908: W/System.err(13583): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-31 13:15:00.908: W/System.err(13583): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-31 13:15:00.908: W/System.err(13583): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 13:15:00.908: W/System.err(13583): at android.os.Looper.loop(Looper.java:137)
01-31 13:15:00.908: W/System.err(13583): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-31 13:15:00.908: W/System.err(13583): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 13:15:00.908: W/System.err(13583): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 13:15:00.908: W/System.err(13583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-31 13:15:00.908: W/System.err(13583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-31 13:15:00.913: W/System.err(13583): at dalvik.system.NativeStart.main(Native Method)

最佳答案

仅供引用,android.os.NetworkOnMainThreadException 会在您在主 UI 线程上执行长时间运行的任务时发生。所以我确定您正在尝试直接在 onCreate() 方法中调用 retrieveInfo()

最好的解决方案是在AsyncTaskdoInBackground() 方法中进行网络调用.还有一点,AsyncTask 在 android 中被称为Painless Threading,因为一旦实现,我们程序员就不需要担心线程的管理。

关于java - 检索 WebService 数组 Java(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574291/

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