我是android的新手,我有一个任务来解析.asmx webservise * (返回JSON *)并在listview中获取它的内容。我从网上查看了几个例子,但这对我没用。
这是我的代码:
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文件但现在我想解析它并将其一些字段存储在listview中。
我的JSON输出如下:
{
"Test0": [
5, - TestId
1, - OrgId
6, - TestType
"a", - TestName
1, - NoOfSection
5, - TestTime
1, - TestScore
"", - Direction
"Y", - Display
"\/Date(1316416540330)\/", - CreatOn
1, - CreatedBy
"N", - ShowCalculator
0, - CourseId
0, - StandardId
0, - SubjectId
"N", - IsConecptBuilder
"Word Test", - TestCategory
"Prospect Test", - TestTyp
19, - StudentId
5, - ReferenceId
"Test", - ReferenceType
0, - GroupId
"\/Date1332924520530)\/", - DataOfAllocation
"Y", - Attended
null, - AttendedOn
13, - AttendedNoOfTime
"Prospect" - StudentType
]
}
任何人都可以帮我修改代码,这样我就可以用以上3个字段填充listview。
谢谢!
答案 0 :(得分:0)
Test0只是一个JSON数组。您可以按ID访问项目。应该像
JSONArray c = JSONArr.getJSONObject(i);
然后访问像
这样的元素int TestId=0;
c.getString(TestId);