android - NetworkOnMainThreadException Error even with AsyncTask -
this question has answer here:
i have problem http post. method works other usage in other classes/activities integrating paypal, don't seem work. because applying in onactivityresult? if so, how rectify this?
attention! edit: believe not duplicate. in codes have included asynctask class call getpaypalresult. have implemented wrongly.
log: error in http connection :android.os.networkonmainthreadexception
public class checkout extends baseactivity implements onclicklistener{ textview f; string min_purchase; string max_purchase; int real_max; int purchaserange; jsonnparser jsonparser = new jsonnparser(); string paypalkeyurl = "https://svcs.sandbox.paypal.com/adaptivepayments/paymentdetails"; string paykey; int fvalue; int minvalue; double thecost; double specialprice; textview theprice; textview t_price; string spaceforprice = " "; private paypal mpaypal; string getprice; private checkoutbutton launchpaypalbutton; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activty_checkout); intent mylocalintent = getintent(); bundle mybundle = mylocalintent.getextras(); theprice = (textview) findviewbyid(r.id.pricetag); theprice.settext(option_price); t_price = (textview) findviewbyid(r.id.totalprice); purchaserange = integer.valueof(min_purchase); minvalue = integer.valueof(min_purchase); f = (textview) findviewbyid(r.id.textview1); f.settext(string.valueof(minvalue)); log.e("oie!!", "min purchase : " + minvalue + " | max purchase : " + max_purchase); getprice = new bigdecimal(string.valueof(theprice.gettext())).multiply(new bigdecimal(string.valueof(f.gettext()))).tostring(); t_price.settext(string.valueof(getprice) + spaceforprice); mpaypal=paypal.initwithappid(this,constants.paypal_app_id,paypal.env_sandbox); mpaypal.setshippingenabled(true); //mpaypal. initui(double.valueof(string.valueof(getprice))); } private void initui(double theprice) { //launchpaypalbutton = mpaypal.getcheckoutbutton(this, // paypal.button_278x43, checkoutbutton.text_pay); launchpaypalbutton = mpaypal.getcheckoutbutton(this, r.drawable.paybutton, checkoutbutton.text_pay); //launchpaypalbutton.setshippingenabled(true); linearlayout.layoutparams params = new linearlayout.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); params.bottommargin = (int) theprice; launchpaypalbutton.setlayoutparams(params); specialprice = theprice; launchpaypalbutton.setonclicklistener(this); string srr=string.valueof(specialprice); ((radiogroup)findviewbyid(r.id.bottommenu)).addview(launchpaypalbutton); linearlayout.layoutparams layoutparams =(linearlayout.layoutparams)launchpaypalbutton.getlayoutparams(); } public void onclick(view v) { paywithpaypal(double.valueof((string.valueof(getprice)))); } public void setcost(double cost) { this.thecost = cost; } public double getcost() { return this.thecost; } private void paywithpaypal(double gg) { paypalpayment newpayment = new paypalpayment(); toast.maketext(getapplicationcontext(),gg.tostring(), toast.length_long).show(); bigdecimal bigdecimal=new bigdecimal(gg); newpayment.setsubtotal(bigdecimal); newpayment.setcurrencytype(currency.getinstance(locale.us)); newpayment.setrecipient("whatad_1358762874_biz@hotmail.com"); newpayment.setmerchantname("my merchant"); intent paypalintent = paypal.getinstance().checkout(newpayment, this); this.startactivityforresult(paypalintent, 1); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch(resultcode) { case activity.result_ok: paykey = data.getstringextra(paypalactivity.extra_pay_key); toast.maketext(this,"paymnet successful : " + paykey,toast.length_long).show(); //finish(); new getpaypalresult().execute(); // break; case activity.result_canceled: toast.maketext(this,"paymnet cancel",toast.length_long).show(); //finish(); break; case paypalactivity.result_failure: toast.maketext(this,"paymnet failed",toast.length_long).show(); string errorid = data.getstringextra(paypalactivity.extra_error_id); string errormessage = data.getstringextra(paypalactivity.extra_error_message); //toast.maketext(getapplicationcontext(),errormessage, toast.length_long).show(); //finish(); break; } } public abslistview.recyclerlistener mrecyclerlistener = new recyclerlistener( ){ public void onmovedtoscrapheap(view view) { viewholder viewholder = (viewholder) view.gettag(); downloadimagetask imagetask = viewholder.mtask; if (imagetask != null) { imagetask.cancel(true); } } }; public string getcontentfromurl(string url, string method, list<namevaluepair> params) { inputstream = null; string result = ""; //toast.maketext(couponview.this,"running this", toast.length_long).show(); // http post try { httppost httppost = new httppost(url); httpresponse response = client.getinstance().execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch (exception e) { log.e("log_tag", "error in http connection :" + e.tostring()); } // convert response string try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { log.e("log_tag", "error converting result " + e.tostring()); } return result; } class getpaypalresult extends asynctask<string, string, string> { listactivity thelist = new listactivity(); private listview mainlistview ; private progressdialog pdialog; /* * * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(checkout.this); pdialog.setmessage("loading options ..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /* * * getting inbox json * */ protected string doinbackground(string... args) { // building parameters return null; } /* * * after completing background task dismiss progress dialog * * */ protected void onpostexecute(string file_url) { mainlistview = (listview) findviewbyid( r.id.list); // dismiss dialog after getting products pdialog.dismiss(); list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("paykey", paykey)); params.add(new basicnamevaluepair("requestenvelope.errorlanguage", "en_us")); httppost.setheader("x-paypal-security-userid", "66.whatever.com"); httppost.setheader("x-paypal-security-password", "6666666"); httppost.setheader("x-paypal-security-signature", "55546g"); httppost.setheader("x-paypal-application-id", "app-80w284485p519543t"); httppost.setheader("x-paypal-request-data-format", "nv"); httppost.setheader("x-paypal-response-data-format", "nv"); string result = getjsonfromurl(paypalkeyurl,params); log.d("paykey content: ", result); } public string getjsonfromurl(string url, list<namevaluepair> params) { inputstream = null; string result = ""; //toast.maketext(couponview.this,"running this", toast.length_long).show(); // http post try { httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params, http.utf_8)); httpresponse response = client.getinstance().execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch (exception e) { log.e("log_tag", "error in http connection :" + e.tostring()); } // convert response string try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { log.e("log_tag", "error converting result " + e.tostring()); } return result; } } }
actually have applied asynctask did wrongly. selvin pointed out move 'getjsonfromurl' call doinbackground. , works!
class getpaypalresult extends asynctask<string, string, string> { listactivity thelist = new listactivity(); private listview mainlistview ; private progressdialog pdialog; /* * * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(checkout.this); pdialog.setmessage("loading options ..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /* * * getting inbox json * */ protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("paykey", paykey)); params.add(new basicnamevaluepair("requestenvelope.errorlanguage", "en_us")); httppost.setheader("x-paypal-security-userid", "66.whatever.com"); httppost.setheader("x-paypal-security-password", "6666666"); httppost.setheader("x-paypal-security-signature", "55546g"); httppost.setheader("x-paypal-application-id", "app-80w284485p519543t"); httppost.setheader("x-paypal-request-data-format", "nv"); httppost.setheader("x-paypal-response-data-format", "nv"); string result = getjsonfromurl(paypalkeyurl,params); log.d("paykey content: ", result); return null; } /* * * after completing background task dismiss progress dialog * * */ protected void onpostexecute(string file_url) { pdialog.dismiss(); } public string getjsonfromurl(string url, list<namevaluepair> params) { inputstream = null; string result = ""; //toast.maketext(couponview.this,"running this", toast.length_long).show(); // http post try { httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params, http.utf_8)); httpresponse response = client.getinstance().execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch (exception e) { log.e("log_tag", "error in http connection :" + e.tostring()); } // convert response string try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { log.e("log_tag", "error converting result " + e.tostring()); } return result; } }
Comments
Post a Comment