Android parse Object KSOAP

Deadly 提交于 2020-01-06 15:51:36

问题


I'd like to know how do I parse the output of SOAP. My current code is the following:

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

The output of the response is this:

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

So, how do I get these values separately?

I just want to let you know that If I change this line:

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

To this:

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

I get the following error:

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

Thanks.


回答1:


From your comments you need to parse the json array.

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



回答2:


Solved thanks to @Raghunandan. Also, it retrieves the right encoding, which is awesome, since that json_encode put special characters on strings.

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


来源:https://stackoverflow.com/questions/19120226/android-parse-object-ksoap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!