Xml parse işlemi yapmayı deniyorum. fakat httpClient.execute(httpPost); metodunda hata alıyorum. genel olarak önerilen ve kullanılan XMLParse() metodunu kullanıyorum. acaba problem ne olabilir. Nasıl çözeceğimi bilemiyorum bilgisi olan biri yardımcı olabilir mi ?
public class XMLParser {
// constructor
public XMLParser() {
}
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//url geliyor
Log.d("LOG", "THIS CODE I SEE in LOGCAT");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
public class XmlParseActivity extends Activity {
static final String URL = "http://dl.dropboxusercontent.com/*******.xml";
static final String KEY_BOOK = "book"; // BIR KAYIT - parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_IMAGE_PATH = "image";
static final String KEY_PDF_PATH = "pdf";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_book);
ArrayList<HashMap<String, String>> booksList = new ArrayList<HashMap<String, String>>();
//hack
XMLParser parser = new XMLParser();
//
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_BOOK);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_IMAGE_PATH, parser.getValue(e, KEY_IMAGE_PATH));
map.put(KEY_PDF_PATH, parser.getValue(e, KEY_PDF_PATH));
// adding HashList to ArrayList
booksList.add(map);
}
list = (ListView) findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(this, booksList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}