-1

如何解析我从 .asmx-webservice 获得的 _json。我得到了 SOAP 响应,现在我想解析这个响应并获取列表视图中的项目

这是我的主要活动

MainActivity.java

package com.Parsing.SOAPParsing;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    private static final String SOAP_ACTION = "http://tempuri.org/GetMyTest";

    private static final String METHOD_NAME = "GetMyTest";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://www.beyyondcareers.com/webservice.asmx";

    TextView tv;
    ArrayList<HashMap<String, String>> testList = new ArrayList<HashMap<String, String>>();

    private String result;

    private JSONArray JSONArr;

    private static final String TAG_TEST_ID = "TestId";
    private static final String TAG_ORG_ID = "OrgId";
    private static final String TAG_TEST_TYPE = "TestType";
    private static final String TAG_TEST_NAME = "TestName";
    private static final String TAG_NO_OF_SECTION = "NoOfSection";
    private static final String TAG_TEST_TIME = "TestTime";
    private static final String TAG_TEST_SCORE = "TestScore";
    private static final String TAG_DIRECTION = "Direction";
    private static final String TAG_DISPLAY = "Display";
    private static final String TAG_CREATE_ON = "CreateOn";
    private static final String TAG_CREATED_BY = "CreatedBy";
    private static final String TAG_SHOW_CALCULATOR = "ShowCalculator";
    private static final String TAG_COURSE_ID = "CourseId";
    private static final String TAG_STANDARD_ID = "StandardId";
    private static final String TAG_SUBJECT_ID = "SubjectId";
    private static final String TAG_IS_CONCEPTE_BUILDER = "IsConceptBuilder";
    private static final String TAG_CATEGORY = "TestCategory";
    private static final String TAG_TEST_TYPE_ONE = "testTyp";
    private static final String TAG_STUDENT_ID = "StudentId";
    private static final String TAG_REF_ID = "ReferenceId";
    private static final String TAG_REF_TYPE = "ReferenceType";
    private static final String TAG_GROUP_ID = "GroupId";
    private static final String TAG_DATE_ALLOCATION = "DateOfAllocation";
    private static final String TAG_ATTENDED = "Attended";
    private static final String TAG_ATTENDED_ON = "AttendedOn";
    private static final String TAG_ATTENDED_NO_OF_TIME = "AttendedNoOfTime";
    private static final String TAG_STUDENT_TYPE = "StudentType";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.textview);

        try {

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

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            result = response.toString();
            // JSONObject json = new JSONObject(response.toString());
            // JSONArray jArray = json.getJSONArray("Test0");
            JSONArr = new JSONArray(result);

            for (int i = 0; i < JSONArr.length(); i++) {
                JSONObject c = JSONArr.getJSONObject(i);

                String testid = c.getString(TAG_TEST_ID);
                String orgid = c.getString(TAG_ORG_ID);
                String testtype = c.getString(TAG_TEST_TYPE);
                String testname = c.getString(TAG_TEST_NAME);
                String noofsection = c.getString(TAG_NO_OF_SECTION);
                String testtime = c.getString(TAG_TEST_TIME);
                String testscore = c.getString(TAG_TEST_SCORE);
                String direction = c.getString(TAG_DIRECTION);
                String display = c.getString(TAG_DISPLAY);
                String createdon = c.getString(TAG_CREATE_ON);
                String createdby = c.getString(TAG_CREATED_BY);
                String showcalc = c.getString(TAG_SHOW_CALCULATOR);
                String courseid = c.getString(TAG_COURSE_ID);
                String standardid = c.getString(TAG_STANDARD_ID);
                String subjectid = c.getString(TAG_SUBJECT_ID);
                String isconceptbuilder = c.getString(TAG_IS_CONCEPTE_BUILDER);
                String testcatag = c.getString(TAG_CATEGORY);
                String testtyp = c.getString(TAG_TEST_TYPE_ONE);
                String studendid = c.getString(TAG_STUDENT_ID);
                String refid = c.getString(TAG_REF_ID);
                String reftype = c.getString(TAG_REF_TYPE);
                String groupid = c.getString(TAG_GROUP_ID);
                String dateofallocation = c.getString(TAG_DATE_ALLOCATION);
                String attended = c.getString(TAG_ATTENDED);
                String attendedon = c.getString(TAG_ATTENDED_ON);
                String attendednooftime = c.getString(TAG_ATTENDED_NO_OF_TIME);
                String studenttype = c.getString(TAG_STUDENT_TYPE);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_TEST_ID, testid);
                map.put(TAG_TEST_TYPE, testtype);
                map.put(TAG_TEST_NAME, testname);

                testList.add(map);
            }
        } catch (Exception e) {
            tv.setText(e.getMessage());
        }

        ListAdapter adapter = new SimpleAdapter(this, testList,
                R.layout.list_item, new String[] { TAG_TEST_ID, TAG_TEST_TYPE,
                        TAG_TEST_NAME }, new int[] { R.id.testid, R.id.type,
                        R.id.testname });

        setListAdapter(adapter);
    }

}


callsoap.java

package com.Parsing.SOAPParsing;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    private static final String SOAP_ACTION = "http://tempuri.org/GetMyTest";

    private static final String METHOD_NAME = "GetMyTest";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://www.beyyondcareers.com/webservice.asmx";

    TextView tv;
    ArrayList<HashMap<String, String>> testList = new ArrayList<HashMap<String, String>>();

    private String result;

    private JSONArray JSONArr;

    private static final String TAG_TEST_ID = "TestId";
    private static final String TAG_ORG_ID = "OrgId";
    private static final String TAG_TEST_TYPE = "TestType";
    private static final String TAG_TEST_NAME = "TestName";
    private static final String TAG_NO_OF_SECTION = "NoOfSection";
    private static final String TAG_TEST_TIME = "TestTime";
    private static final String TAG_TEST_SCORE = "TestScore";
    private static final String TAG_DIRECTION = "Direction";
    private static final String TAG_DISPLAY = "Display";
    private static final String TAG_CREATE_ON = "CreateOn";
    private static final String TAG_CREATED_BY = "CreatedBy";
    private static final String TAG_SHOW_CALCULATOR = "ShowCalculator";
    private static final String TAG_COURSE_ID = "CourseId";
    private static final String TAG_STANDARD_ID = "StandardId";
    private static final String TAG_SUBJECT_ID = "SubjectId";
    private static final String TAG_IS_CONCEPTE_BUILDER = "IsConceptBuilder";
    private static final String TAG_CATEGORY = "TestCategory";
    private static final String TAG_TEST_TYPE_ONE = "testTyp";
    private static final String TAG_STUDENT_ID = "StudentId";
    private static final String TAG_REF_ID = "ReferenceId";
    private static final String TAG_REF_TYPE = "ReferenceType";
    private static final String TAG_GROUP_ID = "GroupId";
    private static final String TAG_DATE_ALLOCATION = "DateOfAllocation";
    private static final String TAG_ATTENDED = "Attended";
    private static final String TAG_ATTENDED_ON = "AttendedOn";
    private static final String TAG_ATTENDED_NO_OF_TIME = "AttendedNoOfTime";
    private static final String TAG_STUDENT_TYPE = "StudentType";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.textview);

        try {

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

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            result = response.toString();
            // JSONObject json = new JSONObject(response.toString());
            // JSONArray jArray = json.getJSONArray("Test0");
            JSONArr = new JSONArray(result);

            for (int i = 0; i < JSONArr.length(); i++) {
                JSONObject c = JSONArr.getJSONObject(i);

                String testid = c.getString(TAG_TEST_ID);
                String orgid = c.getString(TAG_ORG_ID);
                String testtype = c.getString(TAG_TEST_TYPE);
                String testname = c.getString(TAG_TEST_NAME);
                String noofsection = c.getString(TAG_NO_OF_SECTION);
                String testtime = c.getString(TAG_TEST_TIME);
                String testscore = c.getString(TAG_TEST_SCORE);
                String direction = c.getString(TAG_DIRECTION);
                String display = c.getString(TAG_DISPLAY);
                String createdon = c.getString(TAG_CREATE_ON);
                String createdby = c.getString(TAG_CREATED_BY);
                String showcalc = c.getString(TAG_SHOW_CALCULATOR);
                String courseid = c.getString(TAG_COURSE_ID);
                String standardid = c.getString(TAG_STANDARD_ID);
                String subjectid = c.getString(TAG_SUBJECT_ID);
                String isconceptbuilder = c.getString(TAG_IS_CONCEPTE_BUILDER);
                String testcatag = c.getString(TAG_CATEGORY);
                String testtyp = c.getString(TAG_TEST_TYPE_ONE);
                String studendid = c.getString(TAG_STUDENT_ID);
                String refid = c.getString(TAG_REF_ID);
                String reftype = c.getString(TAG_REF_TYPE);
                String groupid = c.getString(TAG_GROUP_ID);
                String dateofallocation = c.getString(TAG_DATE_ALLOCATION);
                String attended = c.getString(TAG_ATTENDED);
                String attendedon = c.getString(TAG_ATTENDED_ON);
                String attendednooftime = c.getString(TAG_ATTENDED_NO_OF_TIME);
                String studenttype = c.getString(TAG_STUDENT_TYPE);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_TEST_ID, testid);
                map.put(TAG_TEST_TYPE, testtype);
                map.put(TAG_TEST_NAME, testname);

                testList.add(map);
            }
        } catch (Exception e) {
            tv.setText(e.getMessage());
        }

        ListAdapter adapter = new SimpleAdapter(this, testList,
                R.layout.list_item, new String[] { TAG_TEST_ID, TAG_TEST_TYPE,
                        TAG_TEST_NAME }, new int[] { R.id.testid, R.id.type,
                        R.id.testname });

        setListAdapter(adapter);
    }

}

从此我得到了json文件,但无法获取列表中的文件...请帮助我

提前致谢/

4

1 回答 1

-1

Google GSON 库将帮助您进行此解析

http://code.google.com/p/google-gson/

于 2012-06-26T11:42:00.197 回答