2012-06-26 64 views
-1

如何解析我從.asmx-webservice獲取的_json。我得到的SOAP響應,現在我要分析此響應,並在ListView具有多個數組的複雜Json解析

在這裏得到的物品是我的主要活動

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文件,但不能讓列表文件中... plz幫助我事先/

感謝

回答

-1

谷歌GSON庫將幫助您在這個分析

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

+0

可以請你給我爲例。如果我的json輸出不能在上面標記,那麼這很重要嗎? –

+0

示例代碼將在網站中出現。不管與標籤。這很簡單,只是讀它你會得到它 –

+0

我沒有找到上述給定的網站上的例子。你可以請給我的鏈接? –