java - Pass global variable to php file through AsyncTask in Android Studio -
i populating rrecyclerview remote database wish query database dates based on users id. have userid assigned global variable on loginactivty of app i'm not sure pass information php page dateactivity.
my code dateactivity follows:
import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.widget.toast; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.arraylist; import java.util.list; public class dateactivity extends appcompatactivity { public static string globex_num; // connection_timeout , read_timeout in milliseconds public static final int connection_timeout = 10000; public static final int read_timeout = 15000; private recyclerview mrvdatelist; private adapterdate madapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_date); //make call asynctask new asyncfetch().execute(); } private class asyncfetch extends asynctask<string, string, string> { progressdialog pdloading = new progressdialog(dateactivity.this); httpurlconnection conn; url url = null; @override protected void onpreexecute() { super.onpreexecute(); //this method running on ui thread pdloading.setmessage("\tloading..."); pdloading.setcancelable(false); pdloading.show(); } @override protected string doinbackground(string... params) { try { url = new url("thephpfile.com"); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); return e.tostring(); } try { // setup httpurlconnection class send , receive data php , mysql conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(read_timeout); conn.setconnecttimeout(connection_timeout); conn.setrequestmethod("get"); // setdooutput true recieve data json file conn.setdooutput(true); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); return e1.tostring(); } try { int response_code = conn.getresponsecode(); // check if successful connection made if (response_code == httpurlconnection.http_ok) { // read data sent server inputstream input = conn.getinputstream(); bufferedreader reader = new bufferedreader(new inputstreamreader(input)); stringbuilder result = new stringbuilder(); string line; while ((line = reader.readline()) != null) { result.append(line); } // pass data onpostexecute method return (result.tostring()); } else { return ("unsuccessful"); } } catch (ioexception e) { e.printstacktrace(); return e.tostring(); } { conn.disconnect(); } } @override protected void onpostexecute(string result) { //this method running on ui thread pdloading.dismiss(); list<datadate> data=new arraylist<>(); pdloading.dismiss(); try { jsonarray jsonarray = new jsonarray(result); // extract data json , store arraylist class objects for(int i=0;i<jsonarray.length();i++){ jsonobject json_data = jsonarray.getjsonobject(i); datadate datedata = new datadate(); datedata.date= json_data.getstring("date"); data.add(datedata); } // setup , handover data recyclerview mrvdatelist = (recyclerview)findviewbyid(r.id.datelist); madapter = new adapterdate(dateactivity.this, data); mrvdatelist.setadapter(madapter); mrvdatelist.setlayoutmanager(new linearlayoutmanager(dateactivity.this)); } catch (jsonexception e) { toast.maketext(dateactivity.this, e.tostring(), toast.length_long).show(); } } } }
i solved problem passing though link.
url = new url(www.thephpfile.php?userid=" + loginactivity.userid);
at start of php file did following:
$userid = $_get['userid'];
Comments
Post a Comment