xml parsing - Android dont get the whole data -
i trying download xml file server , use xmlpull parsher handle doesnt download whole data everytime. if try wait download (thread sleep). have idea why happen or how solved problem? function download file
/*xml read*/ private string downloadurl(string myurl) throws ioexception { inputstream = null; int len = 100000; try { url url_get = new url(myurl); httpurlconnection conn = (httpurlconnection) url_get.openconnection(); conn.setreadtimeout(15000 /* milliseconds */); conn.setconnecttimeout(15000 /* milliseconds */); conn.setrequestmethod("get"); conn.setdoinput(true); // starts query conn.connect(); = conn.getinputstream(); string contentasstring = readit(is, len); return contentasstring; // makes sure inputstream closed after app // finished using it. } { if (is != null) { is.close(); } } } public string readit(inputstream stream, int len) throws ioexception, unsupportedencodingexception { reader reader = null; reader = new inputstreamreader(stream, "utf-8"); char[] buffer = new char[len]; reader.read(buffer); return new string(buffer); }
i using thread start function ofcourse.
here thread:
thread thread = new thread(new runnable() { public void run() { try { thread.sleep(100); while (while_start) { if(while_getadat){ try { get_xml = downloadurl(url_imei); thread.sleep(2000); global_xml_data=red_xml(get_xml); thread.sleep(1000); while_getadat=false; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } } }
update
interesting thigs in debug mode program work properlly got every piece of datas
would consider using asynctask instead of starting thread manually? here working example of posting (vs in question) url:
/** * call restful api post json * @param url * @param jsonobject */ private void postjson(url url, jsonobject jsonobject) { httpurlconnection conn = null; try { conn = (httpurlconnection) url.openconnection(); conn.setrequestproperty("content-type", "application/json"); conn.setdooutput(true); conn.setchunkedstreamingmode(0); conn.setusecaches(false); outputstream out = new bufferedoutputstream(conn.getoutputstream()); string jsonstring = jsonobject.tostring(); log.i("artistregistrationactivity", "sending " + jsonstring + " " + conn.geturl().tostring()); out.write(jsonstring.getbytes()); out.flush(); out.close(); } catch (ioexception e) { log.e("artistregistrationactivity", e.tostring()); } { conn.disconnect(); } } /** * call postjson in separate thread. takes url , jsonobject param */ private class postjsontask extends asynctask<object, void, void> { @override protected void doinbackground(object... urlandjson) { // params[0] url, params[1] json sent. url url = (url) urlandjson[0]; jsonobject json = (jsonobject) urlandjson[1]; postjson(url, json); return null; } }
Comments
Post a Comment