android - When i change my api level my http request doesn't work -
when api level set 8, fine. when try set minsdkversion 11, http request xml doesn't work. got exception on if. here code:
url url; string ciao=""; try { url = new url("myurl"); urlconnection connection = url.openconnection(); httpurlconnection httpconnection = (httpurlconnection)connection; int responsecode = httpconnection.getresponsecode(); if (responsecode == httpurlconnection.http_ok) { inputstream in = httpconnection.getinputstream(); documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); document dom = db.parse(in); domsource domsource = new domsource(dom); stringwriter writer = new stringwriter(); streamresult result = new streamresult(writer); transformerfactory tf = transformerfactory.newinstance(); transformer transformer = tf.newtransformer(); transformer.transform(domsource, result); ciao=writer.tostring(); } } catch(exception e) { } return ciao;
ensure manifest has below statement:
<uses-sdk android:minsdkversion="number-of-version-you-want" />
then
- clean project
- re-build project.
- right click project , select run updated version of apk generated. use apk.
- try uninstalling , re-installing app in mobile phone.
note: use asynctask in order avoid anr , networkonmainthreadexception exception on main thread..
edit: please find code asynctask: have used progressdialog in order display "loading please wait" kind of message in case, url call takes longer time.
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { .... // code here // async task can called within onclick code. new asynctaskoperation().execute(""); } /* async task called avoid android network on main thread exception. web services need consumed in background. */ private class asynctaskoperation extends asynctask <string, void, void> { private progressdialog dialog = new progressdialog(loginactivity.this); string ciao=""; protected void onpreexecute() { // display loading spinner dialog.setmessage("loading... please wait.. "); dialog.setprogressstyle(progressdialog.style_spinner); dialog.setinversebackgroundforced(false); dialog.setcancelable(false); dialog.setindeterminatedrawable(getresources().getdrawable(r.drawable.progressbar_new)); dialog.show(); } @override protected void doinbackground(string... paramsobj) { try { url = new url("myurl"); urlconnection connection = url.openconnection(); httpurlconnection httpconnection = (httpurlconnection)connection; int responsecode = httpconnection.getresponsecode(); if (responsecode == httpurlconnection.http_ok) { inputstream in = httpconnection.getinputstream(); documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); document dom = db.parse(in); domsource domsource = new domsource(dom); stringwriter writer = new stringwriter(); streamresult result = new streamresult(writer); transformerfactory tf = transformerfactory.newinstance(); transformer transformer = tf.newtransformer(); transformer.transform(domsource, result); ciao=writer.tostring(); } } catch(exception e) { } return null; } protected void onpostexecute(void unused) { // close progress dialog dialog.dismiss(); // actions ciao string } // end of method onpostexecute } // end of class asynctaskoperation
}// end of mainactivity
Comments
Post a Comment