1. Download our Official Android App: Forums for Android!

Apps General Http Connection Discussion (not about codes)

Discussion in 'Android Development' started by ac4android, Jun 7, 2016.

  1. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    I am not sure I have chosen the right way to do it.

    Assume the user has a data plan, his personal "hotspot", on the smartphone.

    For example if I use .PHP to _POST data every 30 seconds on the server, and use java.net.URL and java.net.Connection on the Android end, whether it would be better to keep the connection alive on Android and keep the loop running until the user shuts down the app OR re-connect in however many seconds.

    On the server side, it executes a .PHP, grabs the values from the URL and insert them into the database.

    Since we are using 4G LTE, and we have only 4 to 5 variables in the URL-string, it shouldn't be a problem (P.S. I am using T-Mobile).

    But what if the user does not have a data plan or has just exhausted his 7GB data plan? Than he would rely on the relatively slow 4G LTE.

    Also keeping the connection open might be an issue in a production environment with a heavy traffic, especially since the user is driving.

    I am assuming the user plugs his smartphone into the charger in the car. The battery wouldn't last 2 hours.

    Any thoughts and practical experience you might want to share?
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,710
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,710
    9,430
    1,488
    Male
    Software developer
    South West of England
    Is your app transferring data every 30 seconds? If it uses a lot of bandwidth, users won't be happy about it chewing up their data allowance.
     
  3. HeavensSentSword

    HeavensSentSword Well-Known Member
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    Are you tracking the user? You could look at the Google maps api and see how often they send the information about location. But I thinking depending on what you need from the user, sending data that often you should only sent maybe a few numbers nothing over 2-4 kb like a push notification.
     
    ac4android likes this.
  4. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    At the moment, I am looking at using both GPS_PROVIDER and and NETWORK_PROVIDER and found some very interesting facts:
    NETWORK_PROVIDER thru cellular phones is hardcoded in Android SDK to refresh every 45 seconds, that means this piece of code will not generate an error, but will not work as intended either:
    Code (Text):
    1.  
    2. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,10, locationListener);
    3. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10, locationListener);
    4.  
    For better accuracy, use GPS even if it means a much shorter battery life, but I cannot do fancy math averaging both.


    And another curious thing, if I get a GPS signal, then technically I have PERMISSION, but no, I have to require permission as well. Otherwise A/S 2.1.2 will give you a warning, but the app will run happily.

    I check for gps availability, like this:
    Code (Text):
    1.  
    2. boolean gps_on = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    3. if ( gps_on) {
    4. // TODO
    5. }
    6.  
    I still have to check for PERMISSION after requesting it in AndroidManifest, like this:
    Code (Text):
    1.  
    2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    3.  
    One would assume that if I can get GPS, I can use it. No?


    P.S. Apparently only for SDK versions >= 23, then require permission in code, else no.
     
    #4 ac4android, Jun 9, 2016
    Last edited: Jun 9, 2016
  5. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    Not to mention batery life :( :( :(
     
  6. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53

    Yeah, you hit the nail on the head, just sending 3-4 variables in the URL link, notably latitude and longitude. the grunt work is done by .php and sql on the server.
     
  7. HeavensSentSword

    HeavensSentSword Well-Known Member
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    You could just do a simple post to your server. Its small and won't take up much processing power and bandwidth. you can try okhttp but I used the standard httppost methods built into android.
     
    ac4android likes this.
  8. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    It's a bit more than that.

    I'll start a new thread.
     

Share This Page

Loading...