오윤석

api 연결 모듈 개발

......@@ -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"
......
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();
}
}
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;
}
<?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
......@@ -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
......