1. Are you ready for the Galaxy S20? Here is everything we know so far!

Advice and Hi Level Questions on Asynctask, URLs etc

Discussion in 'Android Development' started by ac4android, Jul 13, 2017.

  1. ac4android

    ac4android Well-Known Member
    Thread Starter

    I am trying to understand why it works and best approach.

    I have been either uploading or downloading from a cloud server using Asynctask and spelling out the url-link like this:
    Code (Text):
    2. protected void onPreExecute(){
    3.     String link="http://www.redacted.com/some.php";
    4. }
    5. protected Void doInBackground(String... arg0){
    6.     HttpHelper httphelper = new HttpHelper();
    7.     String DATA_STRING = httphelper.downloadData(link);
    8.     ...
    9. }
    10. protected void onPostExecute(Void result) {
    11.     // display some info on the UI
    12. }
    This time, I have to upload login-params then download a mySQL-dataset. Since I am cut-n-pasting from my old codes, I use a MainActivity to call a pure java helper class which does the grunt work connecting to remote server, like this:
    Code (Text):
    2. URL url = new URL(link);
    3. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    4. conn.setRequestMethod("POST");
    I find it quite difficult in Android to pass the login-params from MainActivity to the helper class and wish I had started from scratch using a LoginActivity instead of a java class, so that in MainActivity, I could use Intent and Bundles to pass those login-params. I am kind of trying to see whether I could turn the helper class into a fragment later on.

    For expediency, I simply appended the login-params to the link, so that it would look like this:
    Code (Text):
    2. link = "http://www.redacted.com/some.php?param1=value1&param2=value2"
    In the end, it all works, I get the formatted info to display on the UI.

    1. Assuming I don't want to end up with a very long url link by appending the params, what would be the best way to pass the params from MainActivity to the helper class?
    2. Assuming I write a LoginActivity, should I just pass the login-params as either EXTRAs or use a BUNDLE, then stick them into an OutputStreamWriter, then flush them?


  2. Best Answer:
    Post #6 by Deleted User, Jul 16, 2017 (1 points)

    1. Download the Forums for Android™ app!


  3. Deleted User

    Deleted User Guest

    Is your Helper class a nested class within MainActivity. If so, it has access to all the member variables of MainActivity. So you can simply set the login parameters up as member variables of MainActivity.
  4. ac4android

    ac4android Well-Known Member
    Thread Starter

    No, it's not an inner class in the MainActivity because that would give me "loopers" error. It's just a helper that other activities can call, and I want to see whether I can turn that into a fragment later on... time permitting.

    In the meantime, I think a second Activity, e.g. LoginActivity, to replace the helper would be best if there are many params??? If there are have a dozen params for the SQL WHERE filter, than the link would be very ugly.
  5. Deleted User

    Deleted User Guest

    Use an ArrayList
  6. ac4android

    ac4android Well-Known Member
    Thread Starter

    Okay, I can use a string array...
    linkArray[0] = "http://www.redacted.com/some.php?"
    linkArray[1] = "bankname=NatWest&"
    linkArray[2] = "IBAN=1234567&"
    Something like that and pass that onto the helper...
    I hope Android java is very private and https is secure and what about encryption beyond base64?
  7. Deleted User

    Deleted User Guest

    Well you can use a simple array, but that limits the number of elements. An ArrayList allows a variable number of elements.
    ac4android likes this.
  8. ac4android

    ac4android Well-Known Member
    Thread Starter

    Thanks, time permitting, I'll start experimenting with the codes, may be Christmas...
    I actually did use a simple String array in keeping with my KISS principle.

    One more question: where best to read up on encryption for people like me?

Share This Page