java - Different HTTP POST in different class using the same methods? -


i pretty new android developement , java. currently, have application makes basic http post request few parameters.

i know if possible make 2 activities make same request different parameters, not having paste sames methods in 2 activities.

example: have 2 screens, identics , when press button on each screen sends post request made different parameters.

ps: ask may not specific enough, ask me details or code (but don't think it's necessary here).

edit : think badly explained thougts :d have class static functions post :

public class myhttppost {

public static string performpostcall(string requesturl, hashmap<string, string> postdataparams) throws ioexception {      inputstream = null;     int len = 500;     url url;      try {         url = new url(requesturl);          httpurlconnection conn = (httpurlconnection) url.openconnection();         conn.setreadtimeout(15000);         conn.setconnecttimeout(15000);         conn.setrequestmethod("post");         conn.setdoinput(true);         conn.setdooutput(true);          outputstream os = conn.getoutputstream();          bufferedwriter writer = new bufferedwriter(                 new outputstreamwriter(os, "utf-8"));         writer.write(getpostdatastring(postdataparams));          writer.flush();         writer.close();         os.close();          = conn.getinputstream();          return readit(is, len);      } {         if (is != null) {             is.close();         }     } }  public static string readit(inputstream stream, int len) throws ioexception {     reader reader = new inputstreamreader(stream, "utf-8");     char[] buffer = new char[len];     reader.read(buffer);     return new string(buffer); }  private static string getpostdatastring(hashmap<string, string> params) throws unsupportedencodingexception {     stringbuilder result = new stringbuilder();     boolean first = true;     for( map.entry<string, string> entry : params.entryset() ) {          if (first)             first = false;         else             result.append("&");          result.append(urlencoder.encode(entry.getkey(), "utf-8"));         result.append("=");         result.append(urlencoder.encode(entry.getvalue(), "utf-8"));     }      return result.tostring(); } 

}

and 2 activities :

public class testpost extends appcompatactivity {

public final static string extra_message = "message";  private textview myview; private edittext urltext; hashmap<string, string> postdataparams; webview webview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_test_post);      myview = (textview)findviewbyid(r.id.mytext);     urltext = (edittext)findviewbyid(r.id.myurl);      postdataparams = new hashmap<>();     postdataparams.put("firstparam", "1234");     postdataparams.put("secondparam", "qwerty");      webview = new webview(this);     webview = (webview) findviewbyid(r.id.mywebview);  }  public void sendmessage(view view) {     intent intent = new intent(this, home.class);      textview edittextview = (textview) findviewbyid(r.id.mytext);     string message = edittextview.gettext().tostring();     intent.putextra(extra_message, message);     startactivity(intent); }  protected class downloadwebpagetask extends asynctask<string, void, string> {     @override     protected string doinbackground(string... urls) {          // params comes execute() call: params[0] url.         try {             return myhttppost.performpostcall(urls[0], postdataparams);         } catch (ioexception e) {              return getresources().getstring(r.string.bad_url);         }     }     // onpostexecute displays results of asynctask.     @override     protected void onpostexecute(string result) {         myview.settext(result);         webview.loaddata(result, "text/html", null);     } }  // when user clicks button, calls asynctask. // before attempting fetch url, makes sure there network connection. public void myclickhandler(view view) {     // gets url ui's text field.     string stringurl = urltext.gettext().tostring();     connectivitymanager connmgr = (connectivitymanager)             getsystemservice(context.connectivity_service);     networkinfo networkinfo = connmgr.getactivenetworkinfo();     if (networkinfo != null && networkinfo.isconnected()) {         new downloadwebpagetask().execute(stringurl);     } else {         myview.settext("no network connection available.");     } }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_test_post, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } 

}

and

public class otherclass extends testpost {

private textview myview; private edittext urltext; hashmap<string, string> postdataparams; webview webview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_test_post);      myview = (textview)findviewbyid(r.id.mytext);     urltext = (edittext)findviewbyid(r.id.myurl);     myview.settext("coucou");      postdataparams = new hashmap<>();     postdataparams.put("firstparam", "9876");     postdataparams.put("secondparam", "ytreza");      webview = new webview(this);     webview = (webview) findviewbyid(r.id.mywebview); }  protected class downloadwebpagetask extends asynctask<string, void, string> {     @override     protected string doinbackground(string... urls) {          // params comes execute() call: params[0] url.         try {             return myhttppost.performpostcall(urls[0], postdataparams);         } catch (ioexception e) {             return getresources().getstring(r.string.bad_url);         }     }     // onpostexecute displays results of asynctask.     @override     protected void onpostexecute(string result) {         myview.settext(result);         webview.loaddata(result, "text/html", null);     } }  public void myclickhandler(view view) {     // gets url ui's text field.     string stringurl = urltext.gettext().tostring();     connectivitymanager connmgr = (connectivitymanager)             getsystemservice(context.connectivity_service);     networkinfo networkinfo = connmgr.getactivenetworkinfo();     if (networkinfo != null && networkinfo.isconnected()) {         new downloadwebpagetask().execute(stringurl);     } else {         myview.settext("no network connection available.");     } } 

}

as see, in second class, send different parameters, must redefine functions, , know if it's option (and if it's not bad so). if in 2 class define params , make request.

if understand well, if create utils class, can paste methods (public , having parameters pass), same both activities , call them activities this: utilsclass.sendpostrequest(myparam1, myparam2);


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -