Toggle navigation
Toggle navigation
This project
Loading...
Sign in
cse437_e
/
smartdoorlock-frontend
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
오윤석
2020-11-05 04:13:04 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b13090b9cb319f2f30e82afc1e2e6095abc49765
b13090b9
1 parent
cd601ef9
api 연결 모듈 개발
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
123 additions
and
1 deletions
app/src/main/AndroidManifest.xml
app/src/main/java/com/sunnni/smartdoorlock/api/Api.java
app/src/main/java/com/sunnni/smartdoorlock/api/ApiResult.java
app/src/main/res/xml/network_security_config.xml
build.gradle
app/src/main/AndroidManifest.xml
View file @
b13090b
...
...
@@ -11,6 +11,7 @@
android:label=
"@string/app_name"
android:roundIcon=
"@mipmap/ic_launcher_round"
android:supportsRtl=
"true"
android:networkSecurityConfig=
"@xml/network_security_config"
android:theme=
"@style/Theme.AppCompat.NoActionBar"
>
<activity
android:name=
".ui.SplashActivity"
android:screenOrientation=
"fullSensor"
...
...
app/src/main/java/com/sunnni/smartdoorlock/api/Api.java
0 → 100644
View file @
b13090b
package
com
.
sunnni
.
smartdoorlock
.
api
;
import
android.os.Handler
;
import
android.util.Log
;
import
com.google.gson.JsonArray
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonObject
;
import
com.google.gson.JsonParser
;
import
org.json.JSONObject
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedReader
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
public
class
Api
{
public
interface
Callback
{
void
callbackMethod
(
Object
obj
);
}
static
private
final
String
DOMAIN
=
"gateway.bu.to:8000"
;
static
private
String
accessToken
=
null
;
static
private
void
callApi
(
final
String
method
,
final
String
endpoint
,
final
JsonObject
params
,
final
Callback
callback
)
{
final
Handler
handler
=
new
Handler
();
new
Thread
()
{
public
void
run
()
{
ApiResult
apiResult
;
try
{
Log
.
d
(
"test"
,
"callApi"
);
URL
url
=
new
URL
(
"http://"
+
Api
.
DOMAIN
+
endpoint
);
HttpURLConnection
conn
=
(
HttpURLConnection
)
url
.
openConnection
();
conn
.
setRequestMethod
(
method
);
if
(
accessToken
!=
null
)
{
conn
.
setRequestProperty
(
"access_token"
,
accessToken
);
}
if
(
"POST"
.
equals
(
method
)
||
"PUT"
.
equals
(
method
))
{
if
(
params
==
null
)
{
throw
new
Exception
(
"params is null"
);
}
OutputStream
os
=
conn
.
getOutputStream
();
os
.
write
(
params
.
toString
().
getBytes
());
os
.
flush
();
os
.
close
();
}
StringBuilder
builder
=
new
StringBuilder
();
InputStream
is
=
conn
.
getInputStream
();
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
is
,
"UTF-8"
));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
builder
.
append
(
line
);
}
Log
.
d
(
"http result"
,
builder
.
toString
());
JsonElement
jsonElement
=
JsonParser
.
parseString
(
builder
.
toString
());
apiResult
=
new
ApiResult
(
true
,
jsonElement
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
apiResult
=
new
ApiResult
(
false
,
e
.
getMessage
());
}
final
ApiResult
finalApiResult
=
apiResult
;
handler
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
callback
.
callbackMethod
(
finalApiResult
);
}
});
}
}.
start
();
}
}
app/src/main/java/com/sunnni/smartdoorlock/api/ApiResult.java
0 → 100644
View file @
b13090b
package
com
.
sunnni
.
smartdoorlock
.
api
;
public
class
ApiResult
{
public
String
getError
()
{
return
this
.
error
;
}
public
boolean
isError
()
{
return
this
.
error
!=
null
;
}
public
Object
getData
()
{
return
this
.
data
;
}
public
void
setError
(
String
error
)
{
this
.
error
=
error
;
}
public
void
setData
(
Object
data
)
{
this
.
data
=
data
;
}
public
ApiResult
(
boolean
result
,
Object
obj
)
{
if
(
result
)
{
this
.
data
=
obj
;
}
else
{
this
.
error
=
(
String
)
obj
;
}
}
private
String
error
;
private
Object
data
;
}
app/src/main/res/xml/network_security_config.xml
0 → 100644
View file @
b13090b
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!--Set application-wide security config using base-config tag.-->
<base-config
cleartextTrafficPermitted=
"true"
/>
</network-security-config>
\ No newline at end of file
build.gradle
View file @
b13090b
...
...
@@ -8,7 +8,7 @@ buildscript {
}
dependencies
{
classpath
'com.android.tools.build:gradle:3.6.
1
'
classpath
'com.android.tools.build:gradle:3.6.
3
'
// NOTE: Do not place your application dependencies here; they belong
...
...
Please
register
or
login
to post a comment