我想将数据集从JSON webservice返回到使用KSoap2的android ListView

时间:2014-04-01 07:33:10

标签: android json web-services listview

我对Android更新鲜。 我需要在KSoap2的帮助下使用JSON webservices从数据集中填充android listview。 我目瞪口呆,但没有得到适当的解决方案或教程。

我试过这个例子,但是把错误抛出为

"the application tracking has stoppped unexpectedly please try again-Force CLose"

这是我的代码:

SessionManager session;
private static final String Soap_Action_JSONEXP = "http://tempuri.org/Cargonet_tracking";
private static final String METHOD_NAME_JSONEXP = "Cargonet_tracking";
private static final String NAMESPACEEXP = "http://tempuri.org/";
private static final String URL_jsEXP = "http://192.168.1.126/testing/Cargo_Tracking.asmx/Cargonet?wsdl";
private String TAG = "Cargo";
//String contentType = "application/json";
ListView lv;
private static String responseJSON;
ProgressBar pg;
Gson gson = new Gson();

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    session = new SessionManager(getActivity());
    session.checkLogin();

    View view = inflater.inflate(R.layout.activity_main, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    lv = (ListView) getActivity().findViewById(R.id.listView1);
    Thread cmdthredlista = new Thread() {@
        Override
        public void run() {
            SoapObject cmdrequest = new SoapObject(NAMESPACEEXP, METHOD_NAME_JSONEXP);
            System.out.println("requsest " + cmdrequest);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(cmdrequest);
            HttpTransportSE ahttptransport = new HttpTransportSE(URL_jsEXP);
            try {
                ahttptransport.call(Soap_Action_JSONEXP, envelope);
                //final SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                responseJSON = response.toString();

                getActivity().runOnUiThread(new Runnable() {@
                    Override
                    public void run() {
                        try {
                            String[] placelist;
                            placelist = gson.fromJson(responseJSON, String[].class);
                            ArrayAdapter < String > cmdlistaadapteri;
                            cmdlistaadapteri = new ArrayAdapter < String > (getActivity(), android.R.layout.activity_list_item, placelist);
                            lv.setAdapter(cmdlistaadapteri);
                        } catch (Exception e) {
                            e.printStackTrace();
                            System.out.println("error ");
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("error ");
            }
        }
    };
    cmdthredlista.start();
}


public String getTAG() {
    return TAG;
}
public void setTAG(String tAG) {
    TAG = tAG;
}

任何人都可以帮助我。

提前致谢

这是我的logcat

""
04-01 13:46:27.556: E/AndroidRuntime(393): FATAL EXCEPTION: main
04-01 13:46:27.556: E/AndroidRuntime(393): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tracking/com.example.tracking.MainActivity}: java.lang.ClassCastException: com.example.tracking.MainActivity cannot be cast to android.app.Activity
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.os.Looper.loop(Looper.java:132)
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.app.ActivityThread.main(ActivityThread.java:4123)
04-01 13:46:27.556: E/AndroidRuntime(393):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 13:46:27.556: E/AndroidRuntime(393):  at java.lang.reflect.Method.invoke(Method.java:491)
04-01 13:46:27.556: E/AndroidRuntime(393):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-01 13:46:27.556: E/AndroidRuntime(393):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-01 13:46:27.556: E/AndroidRuntime(393):  at dalvik.system.NativeStart.main(Native Method)
04-01 13:46:27.556: E/AndroidRuntime(393): Caused by: java.lang.ClassCastException: com.example.tracking.MainActivity cannot be cast to android.app.Activity
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
04-01 13:46:27.556: E/AndroidRuntime(393):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
04-01 13:46:27.556: E/AndroidRuntime(393):  ... 11 more

0 个答案:

没有答案