안드로이드 개발 질문/답변
(글 수 45,052)

위 그림과 같이 org.json.simple.parser 에 JSONParser 클래스가 있음에도 불구하고 참조할 수 없다는 에러가 뜨는 것 같습니다..
이유를 모르겠습니다.. ㅠㅠ 도와주세요.....
private NaverDAO naverDAO = new NaverDAO();
이게 MainActivity.java의 38번째 줄 입니다..
그리고 아래는
NaverDAO.java 소스입니다..
package com.nozisim.pt.dao;
import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List;
import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException;
import android.content.Context;
import com.nozisim.pt.domain.Const; import com.nozisim.pt.domain.LaneVO; import com.nozisim.pt.domain.LocationVO; import com.nozisim.pt.domain.PathVO; import com.nozisim.pt.domain.SubPathVO; import com.nozisim.pt.util.Http; import com.nozisim.pt.util.JsonUtil;
public class NaverDAO {
public Context ctx;
public List<LocationVO> getLocationSearchList(String key) {
List<LocationVO> list = new ArrayList<LocationVO>();
try {
key = URLEncoder.encode(key, "utf-8");
String result = Http.doGet(Const.LOCATION_SEARCH_URL, "menu=route&query=" + key, 3000);
System.out.println(result);
list = parseLocationList(result);
} catch (Exception e) {
e.printStackTrace();
}
return list; }
public List<PathVO> getPathSearchList(LocationVO start, LocationVO destination) {
List<PathVO> list = new ArrayList<PathVO>();
try {
String param = "start=" + start.getX() + "%2C" + start.getY() + "%2C"
+ URLEncoder.encode(start.getName(), "utf-8");
param += "&destination=" + destination.getX() + "%2C" + destination.getY() + "%2C"
+ URLEncoder.encode(destination.getName(), "utf-8");
String result = Http.doGet(Const.PATH_SEARCH_URL, param, 3000); result = correctJson(result); list = parsePathList(result);
} catch (Exception e) {
e.printStackTrace();
}
return list; }
private List<PathVO> parsePathList(String result) {
List<PathVO> list = new ArrayList<PathVO>();
try {
JSONParser parser = new JSONParser(); JSONObject o = (JSONObject)parser.parse(result); o = JsonUtil.getObject(o, "result");
JSONArray pathList = JsonUtil.getArray(o, "path");
for (int i = 0; i < pathList.size(); i++) {
JSONObject joPath = (JSONObject)pathList.get(i);
JSONObject joPathInfo = JsonUtil.getObject(joPath, "info");
PathVO path = new PathVO();
path.setBusStationCount((String)joPathInfo.get("busStationCount"));
path.setBusTransitCount((String)joPathInfo.get("busTransitCount"));
path.setFirstStartStation((String)joPathInfo.get("firstStartStation"));
path.setLastEndStation((String)joPathInfo.get("lastEndStation"));
path.setPathType((String)joPathInfo.get("pathType"));
path.setPayment((String)joPathInfo.get("payment"));
path.setSubwayStationCount((String)joPathInfo.get("subwayStationCount"));
path.setSubwayTransitCount((String)joPathInfo.get("subwayTransitCount"));
path.setTotalDistance((String)joPathInfo.get("totalDistance"));
path.setTotalStationCount((String)joPathInfo.get("totalStationCount"));
path.setTotalTime((String)joPathInfo.get("totalTime"));
path.setTotalWalk((String)joPathInfo.get("totalWalk"));
path.setTrafficDistance((String)joPathInfo.get("trafficDistance"));
JSONArray subPathList = JsonUtil.getArray(joPath, "subPath");
for (int j = 0; j < subPathList.size(); j++) {
JSONObject joSubPath = (JSONObject)subPathList.get(j);
SubPathVO subPathVO = new SubPathVO();
subPathVO.setDistance((String)joSubPath.get("distance"));
subPathVO.setDoor((String)joSubPath.get("door"));
subPathVO.setEndId((String)joSubPath.get("endID"));
subPathVO.setEndName((String)joSubPath.get("endName"));
subPathVO.setEndX((String)joSubPath.get("endX"));
subPathVO.setEndY((String)joSubPath.get("endY"));
subPathVO.setGuide((String)joSubPath.get("guide"));
subPathVO.setSectionTime((String)joSubPath.get("sectionTime"));
subPathVO.setStartId((String)joSubPath.get("startID"));
subPathVO.setStartName((String)joSubPath.get("startName"));
subPathVO.setStartX((String)joSubPath.get("startX"));
subPathVO.setStartY((String)joSubPath.get("startY"));
subPathVO.setStationCount((String)joSubPath.get("stationCount"));
subPathVO.setTrafficType((String)joSubPath.get("trafficType"));
subPathVO.setWay((String)joSubPath.get("way"));
LaneVO lane = new LaneVO();
if (subPathVO.getTrafficType().equals("1")) {
JSONObject joLane = JsonUtil.getObject(joSubPath, "lane");
lane.setName((String)joLane.get("name"));
lane.setSubwayCode((String)joLane.get("subwayCode"));
} else if (subPathVO.getTrafficType().equals("2")) {
JSONObject joLane = JsonUtil.getObject(joSubPath, "lane");
lane.setBusId((String)joLane.get("busID"));
lane.setBusNo((String)joLane.get("busNo"));
lane.setType((String)joLane.get("type"));
}
subPathVO.setLane(lane);
path.addSubPath(subPathVO);
}
list.add(path);
}
} catch (Exception e) {
e.printStackTrace();
}
return list; }
private static List<LocationVO> parseLocationList(String result) {
List<LocationVO> list = new ArrayList<LocationVO>();
try {
JSONParser parser = new JSONParser();
JSONObject o = (JSONObject)parser.parse(result);
o = JsonUtil.getObject(o, "result");
o = JsonUtil.getObject(o, "site");
if (o == null) {
return list;
}
JSONArray array = JsonUtil.getArray(o, "list");
for (int i = 0; i < array.size(); i++) {
JSONObject location = (JSONObject)array.get(i);
LocationVO item = new LocationVO();
item.setName((String)location.get("name"));
item.setAddress((String)location.get("address"));
item.setX((String)location.get("x"));
item.setY((String)location.get("y"));
list.add(item);
}
} catch (ParseException e) {
e.printStackTrace();
}
return list; }
public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
NaverDAO naverDAO = new NaverDAO();
List<LocationVO> start = naverDAO.getLocationSearchList("정자역");
List<LocationVO> end = naverDAO.getLocationSearchList("우장산역");
List<PathVO> list = naverDAO.getPathSearchList(start.get(0), end.get(0));
}
public static String correctJson(String jsonValue) {
// Set set = new HashSet();
// String regex = "[a-zA-Z]+:";
// Pattern p = Pattern.compile(regex);
// Matcher m = p.matcher(jsonValue);
// while (m.find()) {
// String g = m.group();
// if (set.contains(g)) {
// continue;
// }
// set.add(g);
//
// String key = g.replace(":", "");
// jsonValue = jsonValue.replaceAll(g, "\"" + key + "\"" + ":");
// }
jsonValue = jsonValue.replaceAll("([a-zA-Z]+):", "\"$1\":");
// jsonValue = jsonValue.replaceAll("[a-zA-Z]+:", "\"$0\":");
return jsonValue;
}
}
2011.06.07 01:55:43
저도 윗분과 같은 의견입니다.
위에 내용만 가지고는 알 수가 없을 듯합니다.
그리고 Json 이라면 안드로이드에도 기본으로 들어가 있으니, 그걸 사용해서 parsing 하시는건 어떨까요?
2011.06.07 10:22:26
자바 Application 객체를 그대로 안드로이드 객체에서 쓸려고 하니 문제가 생기는 겁니다..NaverDAO 본인이 로직 생각해서 만드신건가요? 자바에서 사용자 정의 Object 만들고 사용하는 공부를 먼저 하신뒤에 NaverDAO 소스를 고치세요..인터넷에서 소스 검색하셔서 그걸 가지고 응용도 안하시고 바로 적용한 티가 나네요..지금 NaverDAO에서는 main 함수가 필요없습니다. 자바 Application일 경우엔 자기 자신을 객체화 해서 main 함수에서 이용하는게 가능하지만..안드로이드에선 Java Application을 객체화 해서 돌리는게 아닙니다..




지금 이 상황은 로그캣 내용만 가지고는 정보가 부족합니다..로그캣 내용 보시면 MainActivity.java 의 38라인에서 발생되었다고 하는데..관련 내용이 올라와 있는것도 없고..리턴된 JSON 문자열이 무엇인지도 모릅니다..조금은 디테일한 정보를 올려주세요..