java - Timeout http request? Android -


i have below code, goes url , pulls xml page; returning calling page; bound listview.

the problem have connection refused, or connection takes while establish. app isn't bringing data 40+ seconds sometimes, waiting successful connection made.

my question is, given below code, how time out httpconnection if has been trying more 3 seconds?

this way, if cannot make successful connection, move onto next url; opposed staying on current 1 time.

any advice helpful!

thanks!

code:

package com.example.directrssread;  import java.io.ioexception; import java.io.stringreader; import java.io.unsupportedencodingexception;  import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.basichttpparams; import org.apache.http.params.httpconnectionparams; import org.apache.http.params.httpparams; import org.apache.http.util.entityutils; 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.util.log;  public class xmlparser {      // constructor     public xmlparser() {      }      /**      * getting xml url making http request      * @param url string      * */     public string getxmlfromurl(string url) {         string xml = null;          try {             // defaulthttpclient             defaulthttpclient httpclient = new defaulthttpclient();             //httppost httppost = new httppost(url);             httpget httppost = new httpget(url);               httpresponse httpresponse = httpclient.execute(httppost);             httpentity httpentity = httpresponse.getentity();             xml = entityutils.tostring(httpentity);          } catch (unsupportedencodingexception e) {                       e.printstacktrace();             return xml;         } catch (clientprotocolexception e) {             e.printstacktrace();             return xml;         } catch (ioexception e) {             e.printstacktrace();             return xml;         }         // return xml         return xml;     }      /**      * getting xml dom element      * @param xml string      * */     public document getdomelement(string xml){         document doc = null;         documentbuilderfactory dbf = documentbuilderfactory.newinstance();         try {              documentbuilder db = dbf.newdocumentbuilder();             dbf.setcoalescing(true);                if (db!=null)             {             inputsource = 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;     }      /** getting node value       * @param elem element       */     public final string getelementvalue2( 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  ){                        if( child.getnodetype() == node.text_node || child.getnodetype() == node.cdata_section_node  ){                                return child.getnodevalue();                       }                   }               }           }           return "";           //return elem.gettextcontent();       }        /**       * getting node value       * @param element node       * @param key string       * */      public string getvalue(element item, string str) {                  nodelist n = item.getelementsbytagname(str);                     return this.getelementvalue2(n.item(0));         }   } 

public string getxmlfromurl(string url) {         string xml = null;          try {         httpparams httpparameters = new basichttpparams();         int timeoutconnection = 3000;         httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection);         defaulthttpclient httpclient = new defaulthttpclient(httpparameters);              //httppost httppost = new httppost(url);             httpget httppost = new httpget(url);               httpresponse httpresponse = httpclient.execute(httppost);             httpentity httpentity = httpresponse.getentity();             xml = entityutils.tostring(httpentity);          } catch (unsupportedencodingexception e) {                       e.printstacktrace();             return xml;         } catch (clientprotocolexception e) {             e.printstacktrace();             return xml;         } catch (ioexception e) {             e.printstacktrace();             return xml;         }         // return xml         return xml;     } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -