gpt4 book ai didi

java - Android解析对象KSOAP

转载 作者:行者123 更新时间:2023-12-01 14:03:37 24 4
gpt4 key购买 nike

我想知道如何解析 SOAP 的输出。我当前的代码如下:

try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);

Object response = (Object) envelope.getResponse();

Log.e("Output:", response.toString());

}catch (Exception e){
Log.e("Error:", e.getCause().toString());
}

响应的输出是这样的:

[{"category_id":767,"parent_id":663,"name":"Walk"}, {"category_id":768,"parent_id":767,"name":"Google"}, {"category_id":764,"parent_id":697,"name":"Yellow"}]

那么,如何分别获取这些值?

我只是想让你知道,如果我更改此行:

Object response = (Object) envelope.getResponse();   

对此:

SoapObject response = (SoapObject) envelope.getResponse();  

我收到以下错误:

10-01 11:28:08.086: E/AndroidRuntime(4600): FATAL EXCEPTION: main 
10-01 11:28:08.086: E/AndroidRuntime(4600): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.WebExample/com.example.WebExample.SyncActivity}: java.lang.NullPointerException
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.os.Looper.loop(Looper.java:137)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-01 11:28:08.086: E/AndroidRuntime(4600): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 11:28:08.086: E/AndroidRuntime(4600): at java.lang.reflect.Method.invoke(Method.java:525)
10-01 11:28:08.086: E/AndroidRuntime(4600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-01 11:28:08.086: E/AndroidRuntime(4600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 11:28:08.086: E/AndroidRuntime(4600): at dalvik.system.NativeStart.main(Native Method)
10-01 11:28:08.086: E/AndroidRuntime(4600): Caused by: java.lang.NullPointerException
10-01 11:28:08.086: E/AndroidRuntime(4600): at
com.example.WebExample.SyncActivity.onCreate(SyncActivity.java:49)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.Activity.performCreate(Activity.java:5133)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-01 11:28:08.086: E/AndroidRuntime(4600): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-01 11:28:08.086: E/AndroidRuntime(4600): ... 11 more

谢谢。

最佳答案

根据您的评论,您需要解析 json 数组。

 JSONArray jr = new JSONArray("jsonstring");
for(int i =0 ; i< jr.length();i++)
{
JSONObject jb =(JSONObject) jr.get(i);
String categoryid = jb.getString("category_id");
String parent_id = jb.getString("parent_id");
String name = jb.getString("name");
}

关于java - Android解析对象KSOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19120226/

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