오윤석

에러 대응을 위해 http status 추가

...@@ -63,8 +63,13 @@ public class Api { ...@@ -63,8 +63,13 @@ public class Api {
63 os.close(); 63 os.close();
64 } 64 }
65 65
66 + int status = conn.getResponseCode();
67 + Log.d("status", String.valueOf(status));
68 + InputStream is = conn.getErrorStream();
69 + if(is == null)
70 + is = conn.getInputStream();
71 +
66 StringBuilder builder = new StringBuilder(); 72 StringBuilder builder = new StringBuilder();
67 - InputStream is = conn.getInputStream();
68 BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 73 BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
69 String line; 74 String line;
70 while((line = reader.readLine()) != null) { 75 while((line = reader.readLine()) != null) {
...@@ -72,13 +77,12 @@ public class Api { ...@@ -72,13 +77,12 @@ public class Api {
72 } 77 }
73 78
74 Log.d("http result", builder.toString()); 79 Log.d("http result", builder.toString());
75 -
76 JsonElement jsonElement = JsonParser.parseString(builder.toString()); 80 JsonElement jsonElement = JsonParser.parseString(builder.toString());
77 81
78 - apiResult = new ApiResult(true, jsonElement); 82 + apiResult = new ApiResult(status < 300, status, jsonElement);
79 } catch (Exception e) { 83 } catch (Exception e) {
80 e.printStackTrace(); 84 e.printStackTrace();
81 - apiResult = new ApiResult(false, e.getMessage()); 85 + apiResult = new ApiResult(false);
82 } 86 }
83 87
84 final ApiResult finalApiResult = apiResult; 88 final ApiResult finalApiResult = apiResult;
......
1 package com.sunnni.smartdoorlock.api; 1 package com.sunnni.smartdoorlock.api;
2 2
3 -public class ApiResult { 3 +import com.google.gson.JsonElement;
4 - public String getError() { 4 +import com.google.gson.JsonObject;
5 - return this.error;
6 - }
7 5
8 - public boolean isError() { 6 +public class ApiResult {
9 - return this.error != null; 7 + public boolean isSuccess() {
8 + return this.success;
10 } 9 }
11 10
12 public Object getData() { 11 public Object getData() {
13 return this.data; 12 return this.data;
14 } 13 }
15 14
16 - public void setError(String error) { 15 + public int getStatus() {
17 - this.error = error; 16 + return this.status;
18 } 17 }
19 18
20 - public void setData(Object data) { 19 + public ApiResult(boolean success, int status, JsonElement obj) {
21 - this.data = data; 20 + this.success = success;
21 + this.data = obj;
22 + this.status = status;
22 } 23 }
23 24
24 - public ApiResult(boolean result, Object obj) { 25 + public ApiResult(boolean success) {
25 - if(result) { 26 + this.success = success;
26 - this.data = obj; 27 + this.data = null;
27 - } else { 28 + this.status = 999;
28 - this.error = (String) obj;
29 - }
30 } 29 }
31 30
32 - private String error; 31 + private boolean success;
33 - private Object data; 32 + private JsonElement data;
33 + private int status;
34 } 34 }
......