@Override
protected ArrayList<Store> doInBackground(Void... params) {
try {
String settingUrl = url;
// connect
long start1 = System.currentTimeMillis();
URL u = new URL(settingUrl.trim());
conn = (HttpURLConnection) u.openConnection();
// setting
conn.setRequestMethod("GET");
JSONObject userAgent = new JSONObject();
userAgent.put("model_no", Build.MODEL);
userAgent.put("os_name", "android");
userAgent.put("os_version", Build.VERSION.RELEASE);
userAgent.put("app_version", "1.0");
conn.setRequestProperty("X-user-agent", userAgent.toString());
long end1 = System.currentTimeMillis();
Log.i("setting time", String.valueOf(end1 - start1));
String response;
long start3 = System.currentTimeMillis();
int responseCode = conn.getResponseCode();
long end3 = System.currentTimeMillis();
Log.i("responseCode time", String.valueOf(end3 - start3));
if(responseCode/100 == 2) {
long start2 = System.currentTimeMillis();
is = conn.getInputStream();
baos = new ByteArrayOutputStream();
byte[] byteBuffer = new byte[1024];
byte[] byteData = null;
int nLength = 0;
while ((nLength = is.read(byteBuffer, 0, byteBuffer.length)) != -1) {
baos.write(byteBuffer, 0, nLength);
}
byteData = baos.toByteArray();
response = new String(byteData);
long end2 = System.currentTimeMillis();
Log.i("save data time", String.valueOf(end2 - start2));
JSONObject responseJSON = new JSONObject(response);
}
}
}
현재 서버에서 JSON 데이터를 받아오기위해 HttpURLConnection 객체를 이용해서 받아옵니다.
AsyncTask를 이용해서 받아오는데요...
이 과정에서 데이터의 양이 많을 때 느린 현상이 있어서 이에관해 질문을 드립니다.
이 코드를 통해 Log를 찍어보면
conn.getResponseCode() 를 호출할 시와,
Log 메세지에 save data time에 진행 되는 시간이 지체가 되는 것으로 측정 되었습니다.
질문)
1. getResponseCode 가 속도가 느린 이유가 있나요? 혹은 다른 방법이 있는지...
2. data를 받아올 때 속도를 향상 시킬 더 좋은 방법이 있는지 궁금합니다.