Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS123-CanteeneoAndroid
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Willard Torres
CS123-CanteeneoAndroid
Commits
89301510
Commit
89301510
authored
Nov 24, 2016
by
lumisce
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor retrofit service
parent
afd1dddb
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
41 deletions
+70
-41
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+1
-0
AppUtils.java
app/src/main/java/com/testapp/AppUtils.java
+57
-0
CustomApplication.java
app/src/main/java/com/testapp/CustomApplication.java
+12
-0
NavDrawerActivity.java
app/src/main/java/com/testapp/NavDrawerActivity.java
+0
-41
No files found.
app/src/main/AndroidManifest.xml
View file @
89301510
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<application
<application
android:name=
".CustomApplication"
android:allowBackup=
"true"
android:allowBackup=
"true"
android:icon=
"@mipmap/ic_launcher"
android:icon=
"@mipmap/ic_launcher"
android:label=
"@string/app_name"
android:label=
"@string/app_name"
...
...
app/src/main/java/com/testapp/AppUtils.java
View file @
89301510
...
@@ -4,7 +4,21 @@ import android.content.Context;
...
@@ -4,7 +4,21 @@ import android.content.Context;
import
android.net.ConnectivityManager
;
import
android.net.ConnectivityManager
;
import
android.net.NetworkInfo
;
import
android.net.NetworkInfo
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
import
java.io.File
;
import
java.io.IOException
;
import
okhttp3.Cache
;
import
okhttp3.Interceptor
;
import
okhttp3.OkHttpClient
;
import
retrofit2.Retrofit
;
import
retrofit2.converter.gson.GsonConverterFactory
;
public
class
AppUtils
{
public
class
AppUtils
{
public
static
CanteeneoApiInterface
service
;
private
static
Context
c
;
public
static
boolean
isNetworkAvailable
(
Context
c
)
{
public
static
boolean
isNetworkAvailable
(
Context
c
)
{
ConnectivityManager
connectivity
=(
ConnectivityManager
)
c
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
ConnectivityManager
connectivity
=(
ConnectivityManager
)
c
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
...
@@ -23,4 +37,47 @@ public class AppUtils {
...
@@ -23,4 +37,47 @@ public class AppUtils {
}
}
return
false
;
return
false
;
}
}
public
static
void
initREST
(
String
serverIP
,
Context
c
)
{
AppUtils
.
c
=
c
;
File
httpCacheDirectory
=
new
File
(
c
.
getCacheDir
(),
"responses"
);
int
cacheSize
=
10
*
1024
*
1024
;
// 10MiB
Cache
cache
=
new
Cache
(
httpCacheDirectory
,
cacheSize
);
OkHttpClient
client
=
new
OkHttpClient
().
newBuilder
()
.
addNetworkInterceptor
(
REWRITE_CACHE_CONTROL_INTERCEPTOR
)
.
cache
(
cache
)
.
build
();
Gson
gson
=
new
GsonBuilder
()
.
setDateFormat
(
"yyyy-MM-dd'T'HH:mm:ssZ"
)
.
create
();
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
"http://"
+
serverIP
+
":5000/"
)
.
client
(
client
)
.
addConverterFactory
(
GsonConverterFactory
.
create
(
gson
))
.
build
();
service
=
retrofit
.
create
(
CanteeneoApiInterface
.
class
);
}
private
static
final
Interceptor
REWRITE_CACHE_CONTROL_INTERCEPTOR
=
new
Interceptor
()
{
@Override
public
okhttp3
.
Response
intercept
(
Chain
chain
)
throws
IOException
{
okhttp3
.
Response
origResponse
=
chain
.
proceed
(
chain
.
request
());
if
(
AppUtils
.
isNetworkAvailable
(
c
))
{
int
maxAge
=
60
;
// read from cache for 1 minute
return
origResponse
.
newBuilder
()
.
header
(
"Cache-Control"
,
"public, max-age="
+
maxAge
)
.
build
();
}
else
{
int
maxStale
=
60
*
60
*
24
*
28
;
// tolerate 4-weeks stale
return
origResponse
.
newBuilder
()
.
header
(
"Cache-Control"
,
"public, only-if-cached, max-stale="
+
maxStale
)
.
build
();
}
}
};
}
}
app/src/main/java/com/testapp/CustomApplication.java
0 → 100644
View file @
89301510
package
com
.
testapp
;
import
android.app.Application
;
public
class
CustomApplication
extends
Application
{
@Override
public
void
onCreate
()
{
super
.
onCreate
();
AppUtils
.
initREST
(
getResources
().
getString
(
R
.
string
.
server_ip
),
getApplicationContext
());
}
}
app/src/main/java/com/testapp/NavDrawerActivity.java
View file @
89301510
...
@@ -56,28 +56,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
...
@@ -56,28 +56,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
super
.
onCreate
(
savedInstanceState
);
File
httpCacheDirectory
=
new
File
(
this
.
getCacheDir
(),
"responses"
);
int
cacheSize
=
10
*
1024
*
1024
;
// 10MiB
Cache
cache
=
new
Cache
(
httpCacheDirectory
,
cacheSize
);
OkHttpClient
client
=
new
OkHttpClient
().
newBuilder
()
.
addNetworkInterceptor
(
REWRITE_CACHE_CONTROL_INTERCEPTOR
)
.
cache
(
cache
)
.
build
();
Gson
gson
=
new
GsonBuilder
()
.
setDateFormat
(
"yyyy-MM-dd'T'HH:mm:ssZ"
)
.
create
();
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
"http://"
+
getResources
().
getString
(
R
.
string
.
server_ip
)
+
":5000/"
)
.
client
(
client
)
.
addConverterFactory
(
GsonConverterFactory
.
create
(
gson
))
.
build
();
service
=
retrofit
.
create
(
CanteeneoApiInterface
.
class
);
c
=
getApplicationContext
();
setContentView
(
R
.
layout
.
activity_nav_drawer
);
setContentView
(
R
.
layout
.
activity_nav_drawer
);
Toolbar
toolbar
=
(
Toolbar
)
findViewById
(
R
.
id
.
toolbar
);
Toolbar
toolbar
=
(
Toolbar
)
findViewById
(
R
.
id
.
toolbar
);
setSupportActionBar
(
toolbar
);
setSupportActionBar
(
toolbar
);
...
@@ -253,23 +231,4 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
...
@@ -253,23 +231,4 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
}
}
});
});
}
}
private
static
final
Interceptor
REWRITE_CACHE_CONTROL_INTERCEPTOR
=
new
Interceptor
()
{
@Override
public
okhttp3
.
Response
intercept
(
Chain
chain
)
throws
IOException
{
okhttp3
.
Response
origResponse
=
chain
.
proceed
(
chain
.
request
());
if
(
AppUtils
.
isNetworkAvailable
(
c
))
{
int
maxAge
=
60
;
// read from cache for 1 minute
return
origResponse
.
newBuilder
()
.
header
(
"Cache-Control"
,
"public, max-age="
+
maxAge
)
.
build
();
}
else
{
int
maxStale
=
60
*
60
*
24
*
28
;
// tolerate 4-weeks stale
return
origResponse
.
newBuilder
()
.
header
(
"Cache-Control"
,
"public, only-if-cached, max-stale="
+
maxStale
)
.
build
();
}
}
};
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment