夥計們,我想嘗試從http post rerquest解析xml並將數值存儲在arraylist中,並將其顯示在列表視圖中。但是ArrayOutOfBoundsException有一個錯誤,解析時沒有錯誤,後期沒有錯誤,但是我找不到錯誤在哪裏?數組越界異常
這裏是我的代碼,請審視一下,給我的建議,以糾正這種:
package com.halosys.HivAtlas;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class FeaturedJobs extends Activity{
static String str;
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> value = new ArrayList<String>();
ListView lv;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.featuredjobs);
lv = (ListView)findViewById(R.id.featuredjobscreen);
TestHttpPost obj = new TestHttpPost();
try {
str = obj.executeHttpPost();
//tv.setText(str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(str));
try {
doc = db.parse(inStream);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NodeList nodeList = (NodeList) doc.getElementsByTagName("member");
for(int index=0; index<nodeList.getLength();index++)
{
Node node = nodeList.item(index);
if(node.getNodeType()==Node.ELEMENT_NODE)
{
Element element = (Element)node;
NodeList nameNode = element.getElementsByTagName("name");
NodeList valueNode = element.getElementsByTagName("value");
//name = new String[nameNode.getLength()];
//value = new String[valueNode.getLength()];
for(int iIndex=0;iIndex<nameNode.getLength();iIndex++)
{
if(nameNode.item(iIndex).getNodeType()==Node.ELEMENT_NODE)
{
Element nameElement = (Element)nameNode.item(iIndex);
String nameEl = nameElement.getFirstChild().getNodeValue().trim();
name.add(nameEl);
//elementArr(name);
System.out.println(name);
}
}
for(int vIndex=0;vIndex<valueNode.getLength();vIndex++)
{
Node nodeV = valueNode.item(vIndex);
if(nodeV.getNodeType()==Node.ELEMENT_NODE)
{
Element valueElement = (Element)nodeV;
NodeList dataNode = valueElement.getElementsByTagName("int");
NodeList dataNode1 = valueElement.getElementsByTagName("string");
for(int dIndex=0;dIndex<dataNode.getLength();dIndex++)
{
if(dataNode.item(dIndex).getNodeType()==Node.ELEMENT_NODE)
{
Element datael = (Element)dataNode.item(dIndex);
String data = datael.getFirstChild().getNodeValue().trim();
value.add(data);
}
}
for(int dIndex=0;dIndex<dataNode1.getLength();dIndex++)
{
if(dataNode1.item(dIndex).getNodeType()==Node.ELEMENT_NODE)
{
Element datael = (Element)dataNode1.item(dIndex);
String data = datael.getFirstChild().getNodeValue().trim();
value.add(data);
}
}
}
}
}
}
for(int u=0;u<name.size();u++)
{
System.out.println(name.get(u));
System.out.println(value.get(u));
}
createList();
//lv.setAdapter(new ArrayAdapter<Object>(FeaturedJobs.this,R.layout.featuredjoblist));
/* ArrayList<HashMap<String,Object>> mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<name.size();i++)
{
map.put("train",name.get(i));
map.put("value",value.get(i));
mylist.add(map);
}
map = new HashMap<String,Object>();
map.put("train", R.drawable.ourcollectionbtn);
mylist.add(map);
map = new HashMap<String,Object>();
map.put("train", R.drawable.postjobbtn);
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.featuredjoblist,
new String[] {"train","value"}, new int[] {R.id.jobtitle,R.id.dept,R.id.desc});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
// lv.setTextFilterEnabled(true);
// lv=getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
System.out.println("Sanat");
Intent intent = new Intent(FeaturedJobs.this,FeaturedJobs.class);
startActivity(intent);
}
});*/
}
public void createList()
{
ArrayList<HashMap<String,Object>> mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<name.size();i++)
{
map.put("train",name.get(i));
map.put("value",value.get(i));
mylist.add(map);
}
/*map = new HashMap<String,Object>();
map.put("train", R.drawable.ourcollectionbtn);
mylist.add(map);
map = new HashMap<String,Object>();
map.put("train", R.drawable.postjobbtn);
mylist.add(map);*/
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.featuredjoblist,
new String[] {"train","value"}, new int[] {R.id.jobtitle,R.id.dept,R.id.desc});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
// lv.setTextFilterEnabled(true);
// lv=getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
System.out.println("Sanat");
Intent intent = new Intent(FeaturedJobs.this,FeaturedJobs.class);
startActivity(intent);
}
});
}
public class TestHttpPost
{
public String executeHttpPost() throws Exception {
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://192.168.0.9/RND/press-hiv/services/xmlrpc");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("type", "fjobs"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
發佈logcat。 – 2011-03-12 06:29:33