서버에서 데이터를 받지 못하는건지... 아니면 받는데 josn형식으로 변환이 안되었다고 하는데... 어디가 에러인지 감이 잡히지 않네요... 급해서.. 도움좀 빨리 부탁드려도 될런지요..!
//여기서부터 서버 연동 소스
private class GetData extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
String errorString = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MultitapActivity.this,
"Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
// mTextViewResult.setText(result);
Log.d(TAG, "response - " + result);
if (result == null){
// mTextViewResult.setText(errorString);
}
else {
mJsonString = result;
showResult();
}
}
@Override
protected String doInBackground(String... params) {
String serverURL = params[0];
try {
URL url = new URL(serverURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.connect();
int responseStatusCode = httpURLConnection.getResponseCode();
Log.d(TAG, "response code - " + responseStatusCode);
InputStream inputStream;
if(responseStatusCode == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
}
else{
inputStream = httpURLConnection.getErrorStream();
}
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while((line = bufferedReader.readLine()) != null){
sb.append(line);
}
bufferedReader.close();
return sb.toString().trim();
} catch (Exception e) {
Log.d(TAG, "InsertData: Error ", e);
errorString = e.toString();
return null;
}
}
}
private void showResult(){
Log.d(TAG, "ddddd");
Log.d(TAG, mJsonString);
try {
JSONObject jsonObject = new JSONObject(mJsonString);
// jsonObject.optJSONArray(mJsonString);
JSONArray jsonArray = jsonObject.getJSONArray(TAG_JSON);
for(int i=0;i<jsonArray.length();i++){
JSONObject item = jsonArray.getJSONObject(i);
String num = item.getString(NUM);
String location = item.getString(LOCATION);
String name1 = item.getString(NAME1);
String a1 = item.getString(mA1);
String flag1 = item.getString(FLAG1);
String name2 = item.getString(NAME2);
String a2 = item.getString(mA2);
String flag2 = item.getString(FLAG2);
Log.d(TAG, "여기까지옴");
hashMap = new HashMap<>();
hashMap.put("Content-Type", "application/json; charset=utf-8");
hashMap.put(NUM, num);
hashMap.put(LOCATION, location);
hashMap.put(NAME1, name1);
hashMap.put(mA1, a1);
hashMap.put(FLAG1, flag1);
hashMap.put(NAME2, name2);
hashMap.put(mA2, a2);
hashMap.put(FLAG2, flag2);
Log.d(TAG, "여기까지옴2");
Log.d(TAG, "num => "+hashMap.get("num").toString());
Log.d(TAG, "location => "+hashMap.get("name2").toString());
Log.d(TAG, "flag2 => "+hashMap.get("flag2").toString());
Log.d(TAG, "flag1 => "+hashMap.get("flag1").toString());
int m1 = Integer.parseInt(hashMap.get("mA1").toString());
int m2 = Integer.parseInt(hashMap.get("mA2").toString());
machValueTxt1.setText(m1 + " W");
machValueTxt2.setText(m2 + " W");
totalUseValueTxt2.setText(m1+m2 + " W");
}
} catch (JSONException e) {
Log.d(TAG, "showResult : ", e);
}
}
}