오윤석

api 연결 모듈 개발

...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
11 android:label="@string/app_name" 11 android:label="@string/app_name"
12 android:roundIcon="@mipmap/ic_launcher_round" 12 android:roundIcon="@mipmap/ic_launcher_round"
13 android:supportsRtl="true" 13 android:supportsRtl="true"
14 + android:networkSecurityConfig="@xml/network_security_config"
14 android:theme="@style/Theme.AppCompat.NoActionBar"> 15 android:theme="@style/Theme.AppCompat.NoActionBar">
15 <activity android:name=".ui.SplashActivity" 16 <activity android:name=".ui.SplashActivity"
16 android:screenOrientation="fullSensor" 17 android:screenOrientation="fullSensor"
......
1 +package com.sunnni.smartdoorlock.api;
2 +
3 +import android.os.Handler;
4 +import android.util.Log;
5 +
6 +import com.google.gson.JsonArray;
7 +import com.google.gson.JsonElement;
8 +import com.google.gson.JsonObject;
9 +import com.google.gson.JsonParser;
10 +
11 +import org.json.JSONObject;
12 +
13 +import java.io.BufferedInputStream;
14 +import java.io.BufferedReader;
15 +import java.io.InputStream;
16 +import java.io.InputStreamReader;
17 +import java.io.OutputStream;
18 +import java.net.HttpURLConnection;
19 +import java.net.URL;
20 +
21 +public class Api {
22 + public interface Callback {
23 + void callbackMethod(Object obj);
24 + }
25 +
26 + static private final String DOMAIN = "gateway.bu.to:8000";
27 + static private String accessToken = null;
28 +
29 + static private void callApi(final String method, final String endpoint, final JsonObject params, final Callback callback) {
30 + final Handler handler = new Handler();
31 + new Thread() {
32 + public void run() {
33 + ApiResult apiResult;
34 + try {
35 + Log.d("test", "callApi");
36 + URL url = new URL("http://" + Api.DOMAIN + endpoint);
37 + HttpURLConnection conn = (HttpURLConnection) url.openConnection();
38 + conn.setRequestMethod(method);
39 +
40 + if(accessToken != null) {
41 + conn.setRequestProperty("access_token", accessToken);
42 + }
43 +
44 + if("POST".equals(method) || "PUT".equals(method)) {
45 + if(params == null) {
46 + throw new Exception("params is null");
47 + }
48 + OutputStream os = conn.getOutputStream();
49 + os.write(params.toString().getBytes());
50 + os.flush();
51 + os.close();
52 + }
53 +
54 + StringBuilder builder = new StringBuilder();
55 + InputStream is = conn.getInputStream();
56 + BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
57 + String line;
58 + while((line = reader.readLine()) != null) {
59 + builder.append(line);
60 + }
61 +
62 + Log.d("http result", builder.toString());
63 +
64 + JsonElement jsonElement = JsonParser.parseString(builder.toString());
65 +
66 + apiResult = new ApiResult(true, jsonElement);
67 + } catch (Exception e) {
68 + e.printStackTrace();
69 + apiResult = new ApiResult(false, e.getMessage());
70 + }
71 +
72 + final ApiResult finalApiResult = apiResult;
73 + handler.post(new Runnable() {
74 + @Override
75 + public void run() {
76 + callback.callbackMethod(finalApiResult);
77 + }
78 + });
79 + }
80 + }.start();
81 + }
82 +}
1 +package com.sunnni.smartdoorlock.api;
2 +
3 +public class ApiResult {
4 + public String getError() {
5 + return this.error;
6 + }
7 +
8 + public boolean isError() {
9 + return this.error != null;
10 + }
11 +
12 + public Object getData() {
13 + return this.data;
14 + }
15 +
16 + public void setError(String error) {
17 + this.error = error;
18 + }
19 +
20 + public void setData(Object data) {
21 + this.data = data;
22 + }
23 +
24 + public ApiResult(boolean result, Object obj) {
25 + if(result) {
26 + this.data = obj;
27 + } else {
28 + this.error = (String) obj;
29 + }
30 + }
31 +
32 + private String error;
33 + private Object data;
34 +}
1 +<?xml version="1.0" encoding="utf-8"?>
2 +<network-security-config>
3 + <!--Set application-wide security config using base-config tag.-->
4 + <base-config cleartextTrafficPermitted="true"/>
5 +</network-security-config>
...\ No newline at end of file ...\ No newline at end of file
...@@ -8,7 +8,7 @@ buildscript { ...@@ -8,7 +8,7 @@ buildscript {
8 8
9 } 9 }
10 dependencies { 10 dependencies {
11 - classpath 'com.android.tools.build:gradle:3.6.1' 11 + classpath 'com.android.tools.build:gradle:3.6.3'
12 12
13 13
14 // NOTE: Do not place your application dependencies here; they belong 14 // NOTE: Do not place your application dependencies here; they belong
......