android - HttpParams is deprecated. What should I do? -
this question has answer here:
i'm trying make login , register on android app. i'm having trouble httpparams. following tutorial on youtube tonikami. i'm new android studio. process know. appreciated. :(
public class serverrequests { progressdialog progressdialog; public static final int connection_timeout = 1000 * 15; public static final string server_address = "http://mywebsite.com/"; public serverrequests(context context) { progressdialog = new progressdialog(context); progressdialog.setcancelable(false); progressdialog.settitle("processing..."); progressdialog.setmessage("please wait..."); } public void storeuserdatainbackground(user user, getusercallback usercallback) { progressdialog.show(); new storeuserdataasynctask(user, usercallback).execute(); } public void fetchuserdataasynctask(user user, getusercallback usercallback) { progressdialog.show(); new fetchuserdataasynctask(user, usercallback).execute(); } /** * parameter sent task upon execution progress published during * background computation result of background computation */ public class storeuserdataasynctask extends asynctask<void, void, void> { user user; getusercallback usercallback; public storeuserdataasynctask(user user, getusercallback usercallback) { this.user = user; this.usercallback = usercallback; } @override protected void doinbackground(void... params) { arraylist<namevaluepair> datatosend = new arraylist<>(); datatosend.add(new basicnamevaluepair("name", user.name)); datatosend.add(new basicnamevaluepair("username", user.username)); datatosend.add(new basicnamevaluepair("password", user.password)); datatosend.add(new basicnamevaluepair("age", user.age + "")); httpparams httprequestparams = gethttprequestparams(); httpclient client = new defaulthttpclient(httprequestparams); httppost post = new httppost(server_address + "register.php"); try { post.setentity(new urlencodedformentity(datatosend)); client.execute(post); } catch (exception e) { e.printstacktrace(); } return null; } private httpparams gethttprequestparams() { httpparams httprequestparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httprequestparams, connection_timeout); httpconnectionparams.setsotimeout(httprequestparams, connection_timeout); return httprequestparams; } @override protected void onpostexecute(void result) { super.onpostexecute(result); progressdialog.dismiss(); usercallback.done(null); } } public class fetchuserdataasynctask extends asynctask<void, void, user> { user user; getusercallback usercallback; public fetchuserdataasynctask(user user, getusercallback usercallback) { this.user = user; this.usercallback = usercallback; } @override protected user doinbackground(void... params) { arraylist<namevaluepair> datatosend = new arraylist<>(); datatosend.add(new basicnamevaluepair("username", user.username)); datatosend.add(new basicnamevaluepair("password", user.password)); httpparams httprequestparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httprequestparams, connection_timeout); httpconnectionparams.setsotimeout(httprequestparams, connection_timeout); httpclient client = new defaulthttpclient(httprequestparams); httppost post = new httppost(server_address + "fetchuserdata.php"); user returneduser = null; try { post.setentity(new urlencodedformentity(datatosend)); httpresponse httpresponse = client.execute(post); httpentity entity = httpresponse.getentity(); string result = entityutils.tostring(entity); jsonobject jobject = new jsonobject(result); if (jobject.length() != 0){ log.v("happened", "2"); string name = jobject.getstring("name"); int age = jobject.getint("age"); returneduser = new user(name, age, user.username, user.password); } } catch (exception e) { e.printstacktrace(); } return returneduser; } @override protected void onpostexecute(user returneduser) { super.onpostexecute(returneduser); progressdialog.dismiss(); usercallback.done(returneduser); } }
}'
url url = new url("http://google.co.in"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("post"); conn.setconnecttimeout(2000); = conn.getinputstream();
use above code instead of depreciated httpparams
and passing post params,
uri.builder builder = new uri.builder(); builder.appendqueryparameter("param1", "paramvalue1"); builder.appendqueryparameter("param2", "paramvalue2"); string query = builder.build().getencodedquery(); outputstream os = conn.getoutputstream(); bufferedwriter writer = new bufferedwriter(new outputstreamwriter(os, "utf-8")); writer.write(query); writer.flush(); writer.close(); os.close(); conn.connect(); = conn.getinputstream();
Comments
Post a Comment