package sy; import java.io.BufferedReader; import java.net.*; import org.json.*; import java.io.*; public class POIService { private static String urlString="https://restapi.amap.com/v3/place/text?"; private String REQUEST_URL; public String getREQUEST_URL() { return REQUEST_URL; } public void setREQUEST_URL(String REQUEST_URL) { //��һ���иĶ� this.REQUEST_URL=REQUEST_URL; } public POIService (String params) { this.REQUEST_URL=urlString+params; } public String getPOIinfo() { StringBuffer resultBuffer=new StringBuffer(); try { URL url=new URL(this.REQUEST_URL); URLConnection connection=url.openConnection(); BufferedReader bReader=new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); String strlineString=null; while ((strlineString=bReader.readLine())!=null) { resultBuffer.append(strlineString); } bReader.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } //System.out.print(resultBuffer.toString()); return resultBuffer.toString(); } public int parseJson(String resultString,int currentPage,int offset) throws Exception{ JSONObject jsonResult=new JSONObject(resultString); int status=jsonResult.getInt("status"); if (status==0) { System.out.print("ʧ��"); return -1; } else { JSONArray poiArr=jsonResult.getJSONArray("pois"); int totalcount = jsonResult.getInt("count"); System.out.print("����"+totalcount+"���"); for (int i = 0; i < poiArr.length(); i++) { JSONObject poiObj=poiArr.getJSONObject(i); String idString=poiObj.getString("idString"); String nameString=poiObj.getString("nameString"); String typeString=poiObj.getString("typeString"); String addressString=poiObj.getString("addressString"); String locationString=poiObj.getString("locationString"); String citynameString=poiObj.getString("citynameString"); String adnameString=poiObj.getString("adnameString"); POI poi=new POI(idString,nameString,typeString,addressString,locationString,citynameString,adnameString); System.out.println(((currentPage-1)*offset+i+1)+"|"+poi.getIdString()+"|"+poi.getNameString()+"|"+poi.getTypeString()+"|"+poi.getAddressString()+"|"+poi.getLocationString()+"|"+poi.getCitynameString()+"|"+poi.getAdnameString()); } System.out.println("------------"); return poiArr.length(); }请帮我检查这份代码的问题
时间: 2023-11-12 14:41:13 浏览: 69
java.io详解
这份代码的问题可能有:
1. 在构造方法中,参数params没有进行空值判断,可能会导致NullPointerException异常;
2. 在getPOIinfo方法中,如果连接失败,异常处理只是简单地打印了错误信息,没有进行更加详细的处理;
3. 在parseJson方法中,如果status为0,虽然输出了"失败",但并没有进行更加详细的处理,例如抛出自定义异常等;
4. 在parseJson方法中,如果jsonResult中没有"pois"字段,会导致JSONException异常,但没有进行异常处理。
阅读全文