Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hds
flexin
android-app
Commits
fb34bf55
Commit
fb34bf55
authored
Oct 28, 2017
by
Nastuzzi Samy
Browse files
Optimization
Make the difference between response types Cleaning Optimization
parent
d4bd2b12
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/fr/utc/simde/payutc/BaseActivity.java
View file @
fb34bf55
...
...
@@ -60,9 +60,9 @@ public abstract class BaseActivity extends NFCActivity {
Thread
.
sleep
(
100
);
final
HTTPRequest
request
=
nemopaySession
.
getRequest
();
final
JsonNode
foundationList
=
request
.
getJ
son
Response
();
final
JsonNode
foundationList
=
request
.
getJ
SON
Response
();
if
(!
request
.
isJ
son
Response
()
||
!
foundationList
.
isArray
())
if
(!
request
.
isJ
SON
Response
()
||
!
foundationList
.
isArray
())
throw
new
Exception
(
"Malformed JSON"
);
if
(
foundationList
.
size
()
==
0
)
{
...
...
@@ -131,9 +131,9 @@ public abstract class BaseActivity extends NFCActivity {
Thread
.
sleep
(
100
);
final
HTTPRequest
request
=
nemopaySession
.
getRequest
();
final
JsonNode
categoryList
=
request
.
getJ
son
Response
();
final
JsonNode
categoryList
=
request
.
getJ
SON
Response
();
if
(!
request
.
isJ
son
Response
()
||
!
categoryList
.
isArray
())
if
(!
request
.
isJ
SON
Response
()
||
!
categoryList
.
isArray
())
throw
new
Exception
(
"Malformed JSON"
);
if
(
categoryList
.
size
()
==
0
)
{
...
...
@@ -181,9 +181,9 @@ public abstract class BaseActivity extends NFCActivity {
Thread
.
sleep
(
100
);
final
HTTPRequest
request
=
nemopaySession
.
getRequest
();
final
JsonNode
articleList
=
request
.
getJ
son
Response
();
final
JsonNode
articleList
=
request
.
getJ
SON
Response
();
if
(!
request
.
isJ
son
Response
()
||
!
articleList
.
isArray
())
if
(!
request
.
isJ
SON
Response
()
||
!
articleList
.
isArray
())
throw
new
Exception
(
"Malformed JSON"
);
if
(
articleList
.
size
()
==
0
)
{
...
...
app/src/main/java/fr/utc/simde/payutc/MainActivity.java
View file @
fb34bf55
package
fr.utc.simde.payutc
;
import
android.annotation.SuppressLint
;
import
android.app.Activity
;
import
android.app.ProgressDialog
;
import
android.content.DialogInterface
;
import
android.content.SharedPreferences
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.os.AsyncTask
;
import
android.support.v7.app.AlertDialog
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.EditText
;
import
android.widget.ImageView
;
import
android.widget.TextView
;
import
android.widget.Toast
;
import
java.io.InputStream
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Locale
;
...
...
app/src/main/java/fr/utc/simde/payutc/tools/HTTPRequest.java
View file @
fb34bf55
...
...
@@ -4,6 +4,8 @@ package fr.utc.simde.payutc.tools;
* Created by Samy on 24/10/2017.
*/
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.util.Log
;
import
com.fasterxml.jackson.databind.JsonNode
;
...
...
@@ -13,6 +15,7 @@ import org.json.JSONException;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.UnsupportedEncodingException
;
import
java.net.HttpURLConnection
;
...
...
@@ -26,7 +29,11 @@ public class HTTPRequest {
private
static
final
String
LOG_TAG
=
"_HTTPRequest"
;
private
String
url
;
private
HttpURLConnection
request
;
private
String
response
;
private
String
responseType
;
private
String
StringResponse
;
private
JsonNode
JSONResponse
;
private
Bitmap
BitmapResponse
;
private
Map
<
String
,
String
>
postArgs
;
private
Map
<
String
,
String
>
getArgs
;
...
...
@@ -35,7 +42,8 @@ public class HTTPRequest {
public
HTTPRequest
(
final
String
url
)
{
this
.
url
=
url
;
this
.
request
=
null
;
this
.
response
=
""
;
this
.
responseType
=
"string"
;
this
.
StringResponse
=
""
;
this
.
postArgs
=
new
HashMap
<
String
,
String
>();
this
.
getArgs
=
new
HashMap
<
String
,
String
>();
this
.
cookies
=
new
HashMap
<
String
,
String
>();
...
...
@@ -55,14 +63,14 @@ public class HTTPRequest {
try
{
this
.
request
=
(
HttpURLConnection
)
(
new
URL
(
this
.
url
+
get
)).
openConnection
();
this
.
request
.
setRequestMethod
(
"GET"
);
this
.
request
.
setRequestProperty
(
"Cookie"
,
getCookiesHeader
());
//this.request.setRequestMethod("GET");
//this.request.setRequestProperty("User-Agent", "Application PayUTC");
//this.request.setRequestProperty("Cookie", getCookiesHeader());
this
.
request
.
setUseCaches
(
false
);
this
.
request
.
setDo
Out
put
(
true
);
this
.
request
.
setDo
In
put
(
true
);
updateCookies
(
this
.
request
.
getHeaderFields
().
get
(
"Set-Cookie"
));
generateResponse
();
Log
.
d
(
LOG_TAG
,
"code: "
+
Integer
.
toString
(
this
.
request
.
getResponseCode
())
+
", response: "
+
this
.
getResponse
());
}
catch
(
Exception
e
)
{
try
{
...
...
@@ -104,7 +112,6 @@ public class HTTPRequest {
updateCookies
(
this
.
request
.
getHeaderFields
().
get
(
"Set-Cookie"
));
generateResponse
();
Log
.
d
(
LOG_TAG
,
"code: "
+
Integer
.
toString
(
this
.
request
.
getResponseCode
())
+
", response: "
+
this
.
getResponse
());
}
catch
(
Exception
e
)
{
try
{
...
...
@@ -160,47 +167,94 @@ public class HTTPRequest {
return
""
;
}
protected
void
generateResponse
()
throws
IOException
{
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
this
.
request
.
getInputStream
(),
"UTF-8"
));
StringBuilder
builder
=
new
StringBuilder
();
String
inputLine
;
protected
void
generateResponse
()
throws
Exception
{
InputStream
inputStream
=
this
.
request
.
getInputStream
();
while
((
inputLine
=
in
.
readLine
())
!=
null
)
builder
.
append
(
inputLine
);
in
.
close
();
this
.
response
=
builder
.
toString
();
}
public
Boolean
isJsonResponse
()
throws
Exception
{
if
(
request
==
null
)
return
null
;
if
(
request
.
getContentType
().
equals
(
"application/json"
))
{
this
.
responseType
=
"string"
;
if
(
this
.
request
.
getContentType
().
contains
(
"image/"
))
{
try
{
new
ObjectMapper
().
readTree
(
this
.
response
);
return
true
;
}
catch
(
IOException
e
)
{
this
.
BitmapResponse
=
BitmapFactory
.
decodeStream
(
inputStream
);
this
.
responseType
=
"image"
;
}
catch
(
Exception
e
)
{
this
.
BitmapResponse
=
null
;
throw
new
Exception
(
"Malformed JSON"
);
}
}
else
{
return
false
;
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
,
"UTF-8"
));
StringBuilder
builder
=
new
StringBuilder
();
String
inputLine
;
while
((
inputLine
=
in
.
readLine
())
!=
null
)
{
builder
.
append
(
inputLine
);
}
in
.
close
();
this
.
StringResponse
=
builder
.
toString
();
if
(
this
.
request
.
getContentType
().
equals
(
"application/json"
))
{
try
{
this
.
JSONResponse
=
new
ObjectMapper
().
readTree
(
this
.
StringResponse
);
this
.
responseType
=
"JSON"
;
}
catch
(
Exception
e
)
{
this
.
JSONResponse
=
null
;
throw
new
Exception
(
"Malformed JSON"
);
}
}
}
Log
.
d
(
LOG_TAG
,
"code: "
+
Integer
.
toString
(
this
.
request
.
getResponseCode
())
+
", type: "
+
this
.
responseType
+
(
this
.
responseType
==
"image"
?
""
:
", response: "
+
this
.
getResponse
()));
}
public
JsonNode
getJsonResponse
()
throws
IOException
,
JSONException
{
try
{
return
new
ObjectMapper
().
readTree
(
this
.
response
);
}
catch
(
Exception
e
)
{
Log
.
e
(
LOG_TAG
,
"error: "
+
e
.
getMessage
());
return
null
;
}
public
Boolean
isJSONResponse
()
throws
Exception
{
if
(
this
.
request
==
null
)
throw
new
Exception
(
"No request"
);
return
this
.
responseType
.
equals
(
"JSON"
);
}
public
Boolean
isImageResponse
()
throws
Exception
{
if
(
this
.
request
==
null
)
throw
new
Exception
(
"No request"
);
return
this
.
responseType
.
equals
(
"image"
);
}
public
String
getResponseType
()
throws
Exception
{
if
(
this
.
request
==
null
)
throw
new
Exception
(
"No request"
);
return
this
.
responseType
;
}
public
JsonNode
getJSONResponse
()
throws
Exception
{
if
(
this
.
request
==
null
)
throw
new
Exception
(
"No request"
);
if
(!
this
.
responseType
.
equals
(
"JSON"
))
throw
new
Exception
(
"Not a JSON response"
);
return
this
.
JSONResponse
;
}
public
Bitmap
getImageResponse
()
throws
Exception
{
if
(
this
.
request
==
null
)
throw
new
Exception
(
"No request"
);
if
(!
this
.
responseType
.
equals
(
"image"
))
throw
new
Exception
(
"Not an image response"
);
return
this
.
BitmapResponse
;
}
public
String
getResponse
()
throws
IOException
{
return
response
;
}
public
String
getResponse
()
throws
Exception
{
if
(
this
.
request
==
null
)
throw
new
Exception
(
"No request"
);
return
this
.
StringResponse
;
}
protected
String
args2String
(
Map
<
String
,
String
>
args
)
throws
UnsupportedEncodingException
{
return
args2String
(
args
,
false
);
}
protected
String
args2String
(
Map
<
String
,
String
>
args
,
Boolean
isGet
)
throws
UnsupportedEncodingException
{
...
...
app/src/main/java/fr/utc/simde/payutc/tools/NemopaySession.java
View file @
fb34bf55
...
...
@@ -151,10 +151,10 @@ public class NemopaySession {
}}
);
if
(
reponseCode
!=
200
||
!
this
.
request
.
isJ
son
Response
())
if
(
reponseCode
!=
200
||
!
this
.
request
.
isJ
SON
Response
())
throw
new
Exception
(
"Not created"
);
JsonNode
response
=
this
.
request
.
getJ
son
Response
();
JsonNode
response
=
this
.
request
.
getJ
SON
Response
();
if
(
response
.
has
(
"app_key"
))
this
.
key
=
response
.
get
(
"app_key"
).
textValue
();
else
...
...
@@ -166,7 +166,7 @@ public class NemopaySession {
public
int
loginApp
(
final
String
key
,
CASConnexion
casConnexion
)
throws
Exception
{
int
reponseCode
=
loginApp
(
key
);
JsonNode
response
=
this
.
request
.
getJ
son
Response
();
JsonNode
response
=
this
.
request
.
getJ
SON
Response
();
if
(
response
.
has
(
"config"
)
&&
response
.
get
(
"config"
).
has
(
"cas_url"
))
casConnexion
.
setUrl
(
response
.
get
(
"config"
).
get
(
"cas_url"
).
textValue
());
else
...
...
@@ -184,8 +184,8 @@ public class NemopaySession {
);
JsonNode
response
;
if
(
reponseCode
==
200
&&
this
.
request
.
isJ
son
Response
())
response
=
this
.
request
.
getJ
son
Response
();
if
(
reponseCode
==
200
&&
this
.
request
.
isJ
SON
Response
())
response
=
this
.
request
.
getJ
SON
Response
();
else
throw
new
Exception
(
"Not authentified"
);
...
...
@@ -217,8 +217,8 @@ public class NemopaySession {
);
JsonNode
response
;
if
(
reponseCode
==
200
&&
this
.
request
.
isJ
son
Response
())
response
=
this
.
request
.
getJ
son
Response
();
if
(
reponseCode
==
200
&&
this
.
request
.
isJ
SON
Response
())
response
=
this
.
request
.
getJ
SON
Response
();
else
throw
new
Exception
(
"Not connected"
);
...
...
@@ -243,8 +243,8 @@ public class NemopaySession {
);
JsonNode
response
;
if
(
reponseCode
==
200
&&
this
.
request
.
isJ
son
Response
())
response
=
this
.
request
.
getJ
son
Response
();
if
(
reponseCode
==
200
&&
this
.
request
.
isJ
SON
Response
())
response
=
this
.
request
.
getJ
SON
Response
();
else
throw
new
Exception
(
"Not connected"
);
...
...
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