java - Android AsyncTask cannot pass values to database -
this class contains recyclerview. want when leftswipe on object of list, should make post call local host php page adds values swiped object entry table in mysql database. however, servicehandler class not able pass variables php page add entry.
public class productadapter extends recyclerview.adapter<productadapter.viewholder> { private arraylist<productinfo> inf; productadapter selfref=this; string url_add_product = "http://10.0.2.2/finalproject/add_basket_product.php"; private progressdialog pdialog; private static final string tag_success = "success"; public string nm,pr,dis,st; //helper method productinfo object private productinfo getinfo(string name){ productinfo pinfo=null; for(productinfo x:inf){ if(x.getname().equalsignorecase(name)) pinfo=x; } return pinfo; } public class viewholder extends recyclerview.viewholder { textview pname; textview pprice; textview adistance; textview astore; private context ctx=null; onswipetouchlistener onswipetouchlistener; productinfo p; public viewholder(final view view){ super(view); ctx=view.getcontext(); pname = (textview) view.findviewbyid(r.id.selected_prod_name); pprice = (textview) view.findviewbyid(r.id.selected_prod_price); adistance = (textview) view.findviewbyid(r.id.product_distance); astore = (textview) view.findviewbyid(r.id.product_store); onswipetouchlistener=(new onswipetouchlistener(ctx) { public void onswipetop() { toast.maketext(ctx, "top", toast.length_short).show(); } public void onswiperight() { toast.maketext(ctx, "right", toast.length_short).show(); } public void onswipeleft() { toast.maketext(ctx, "added cart", toast.length_short).show(); // p.setname(pname.gettext().tostring()); nm=(pname.gettext().tostring()); // p.setprice(double.parsedouble(pprice.gettext().tostring())); pr=(pprice.gettext().tostring()); // p.setdistance(double.parsedouble(adistance.gettext().tostring())); dis=adistance.gettext().tostring(); // p.setstore(astore.gettext().tostring()); st=astore.gettext().tostring(); // add product basket activity // string jsonstr=""; // servicehandler sh = new servicehandler(); /* list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("name", pinfo.getname())); params.add(new basicnamevaluepair("price", double.tostring(pinfo.getprice()))); params.add(new basicnamevaluepair("description", pinfo.getname())); params.add(new basicnamevaluepair("location", pinfo.getstore())); params.add(new basicnamevaluepair("distance", double.tostring(pinfo.getdistance()))); jsonstr=sh.makeservicecall(url_add_product,servicehandler.post,params); */ new createnewproduct().execute(); } public void onswipebottom() { toast.maketext(ctx, "bottom", toast.length_short).show(); } @override public void onclick() { super.onclick(); intent i; productinfo pinfo= getinfo(pname.gettext().tostring()); toast.maketext(view.getcontext(),pname.gettext().tostring(),toast.length_long).show(); string name=pinfo.getname(); string price=double.tostring(pinfo.getprice()); string store=pinfo.getstore(); string dist=double.tostring(pinfo.getdistance()); i=new intent(ctx,productlocation.class); bundle b=new bundle(); b.putstring("prod_name",name); b.putstring("prod_price",price); b.putstring("prod_store",store); b.putstring("prod_dist",dist); i.putextras(b); view.getcontext().startactivity(i); } }); //to set onclick listener each item held viewholder view.setontouchlistener(onswipetouchlistener); } class createnewproduct extends asynctask<string, string, string> { string name; string price; string location; string distance; string description; /** * before starting background thread show progress dialog */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(ctx); pdialog.setmessage("creating product.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); name =nm;// p.getname(); price =pr;// double.tostring(p.getprice()); location=st ;// p.getstore(); distance=dis;//double.tostring(p.getdistance()); description = "yellow";// p.getname(); } /** * creating product */ protected string doinbackground(string... args) { string jsonstr=""; try { servicehandler sh = new servicehandler(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("name", name)); params.add(new basicnamevaluepair("price", price)); params.add(new basicnamevaluepair("description", description)); params.add(new basicnamevaluepair("location", location)); params.add(new basicnamevaluepair("distance", distance)); log.d("create response", name); jsonstr = sh.makeservicecall(url_add_product, servicehandler.post, params); // check log cat fro response log.d("create response", jsonstr.tostring()); }catch(exception e){ } // check success tag return "success"; } /** * after completing background task dismiss progress dialog **/ protected void onpostexecute(void result) { // dismiss dialog once done // dismiss progress dialog if (pdialog.isshowing()) pdialog.dismiss(); } } } public productadapter(arraylist<productinfo> prinfo){ this.inf = prinfo; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype){ view itemview = layoutinflater.from(parent.getcontext()).inflate(r.layout.product_card,parent,false); return new viewholder(itemview); } @override public void onbindviewholder(viewholder holder, int position){ productinfo product = inf.get(position); holder.pname.settext(product.getname()); double n = product.getprice(); string s = string.valueof(n) + " hkd"; holder.pprice.settext(s); double m = product.getdistance(); string q = string.valueof(m) + " km"; holder.adistance.settext(q); holder.astore.settext(product.getstore()); } @override public int getitemcount(){ return inf.size(); } }
my servicehandler class follows:
package com.example.dayle_fernandes.final_project; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import java.io.ioexception; import java.io.unsupportedencodingexception; import java.util.list; public class servicehandler { static string response = null; public final static int = 1; public final static int post = 2; public servicehandler() { } /** * making service call * @url - url make request * @method - http request method * */ public string makeservicecall(string url, int method) { return this.makeservicecall(url, method, null); } /** * making service call * @url - url make request * @method - http request method * @params - http request params * */ public string makeservicecall(string url, int method, list<namevaluepair> params) { try { // http client defaulthttpclient httpclient = new defaulthttpclient(); httpentity httpentity = null; httpresponse httpresponse = null; // checking http request method type if (method == post) { httppost httppost = new httppost(url); // adding post params if (params != null) { httppost.setentity(new urlencodedformentity(params)); } httpresponse = httpclient.execute(httppost); return "success"; } else if (method == get) { // appending params url if (params != null) { string paramstring = urlencodedutils .format(params, "utf-8"); url += "?" + paramstring; } httpget httpget = new httpget(url); httpresponse = httpclient.execute(httpget); } httpentity = httpresponse.getentity(); response = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return response; } }
my php file add_basket_product.php is
<?php // array json response $response = array(); // check required fields if (isset($_post['name']) && isset($_post['price']) && isset($_post['description']) && isset($_post['location']) && isset($_post['distance'])) { $name = $_post['name']; $price = $_post['price']; $description = $_post['description']; $location = $_post['location']; $distance = $_post['distance']; $created_at = $_post['created_at']; $updated_at = $_post['updated_at']; // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); // mysql inserting new row $result = mysql_query("insert products(name, price, location, distance, description) values('$name', '$price', '$location', '$distance', '$description')"); // check if row inserted or not if ($result) { // inserted database $response["success"] = 1; $response["message"] = "product created."; // echoing json response echo json_encode($response); } else { // failed insert row $response["success"] = 0; $response["message"] = "oops! error occurred."; // echoing json response echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing"; // echoing json response echo json_encode($response); } ?>
i dont know why not passing . on log activity in doinbackground() - log.d("create response", name); , can see can access name cant make pass php file
Comments
Post a Comment