오윤석

에러 대응을 위해 http status 추가

......@@ -63,8 +63,13 @@ public class Api {
os.close();
}
int status = conn.getResponseCode();
Log.d("status", String.valueOf(status));
InputStream is = conn.getErrorStream();
if(is == null)
is = conn.getInputStream();
StringBuilder builder = new StringBuilder();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while((line = reader.readLine()) != null) {
......@@ -72,13 +77,12 @@ public class Api {
}
Log.d("http result", builder.toString());
JsonElement jsonElement = JsonParser.parseString(builder.toString());
apiResult = new ApiResult(true, jsonElement);
apiResult = new ApiResult(status < 300, status, jsonElement);
} catch (Exception e) {
e.printStackTrace();
apiResult = new ApiResult(false, e.getMessage());
apiResult = new ApiResult(false);
}
final ApiResult finalApiResult = apiResult;
......
package com.sunnni.smartdoorlock.api;
public class ApiResult {
public String getError() {
return this.error;
}
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public boolean isError() {
return this.error != null;
public class ApiResult {
public boolean isSuccess() {
return this.success;
}
public Object getData() {
return this.data;
}
public void setError(String error) {
this.error = error;
public int getStatus() {
return this.status;
}
public void setData(Object data) {
this.data = data;
public ApiResult(boolean success, int status, JsonElement obj) {
this.success = success;
this.data = obj;
this.status = status;
}
public ApiResult(boolean result, Object obj) {
if(result) {
this.data = obj;
} else {
this.error = (String) obj;
}
public ApiResult(boolean success) {
this.success = success;
this.data = null;
this.status = 999;
}
private String error;
private Object data;
private boolean success;
private JsonElement data;
private int status;
}
......