Android解析对象KSOAP

时间:2013-10-01 15:30:03

标签: java android parsing ksoap2

我想知道如何解析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, “姓名”: “走”},   { “CATEGORY_ID”:768, “PARENT_ID”:767, “名”: “谷歌”},   { “CATEGORY_ID”:764, “PARENT_ID”:697, “姓名”: “黄色”}]

那么,我如何单独获取这些值?

我只想告诉你,如果我改变这一行:

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

感谢。

2 个答案:

答案 0 :(得分:1)

从你的评论中你需要解析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");
 }

答案 1 :(得分:0)

感谢 @Raghunandan 。此外,它检索正确的编码,这很棒,因为json_encode在字符串上放置了特殊字符。

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);

    SoapObject rep = (SoapObject) envelope.bodyIn;

    JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
    for(int i =0 ; i< jr.length();i++)
     {
         JSONObject jb =(JSONObject) jr.get(i);  

         String name = jb.getString("name");
         Log.e("value:", name);
     }

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