我正在java中创建一个应用程序,我需要获取csv文件中列出的某些位置的纬度和经度。一切都很顺利,但一段时间后响应就会停止。有时它会解析整套位置,有时会在两者之间停止。我无法抓住这个漏洞,请帮助我。代码写在下面
`int count=0;
JFileChooser jfc;
String path_of_chosen_file;
File directory_of_chosen_file;
ArrayList<String> locations;
FileWriter writer = null;
private void browseBtnActionPerformed(java.awt.event.ActionEvent evt) {
jfc = new JFileChooser();
if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//code to handle choosed file here.
jfilenametxt.setText(jfc.getName(jfc.getSelectedFile()));
path_of_chosen_file = jfc.getSelectedFile().getAbsolutePath();
directory_of_chosen_file=jfc.getCurrentDirectory();
System.out.println(path_of_chosen_file);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
FileInputStream fileIn = null;
locations=new ArrayList();
Scanner scanner = null;
try {
scanner = new Scanner(new File(path_of_chosen_file));
scanner.useDelimiter(",");
while(scanner.hasNextLine())
{
locations.add(scanner.nextLine());
}
locations.removeAll(Arrays.asList("", null));
}
catch(FileNotFoundException e1){
}
String writepath=directory_of_chosen_file.toString()
+"/newfile"+jfc.getName(jfc.getSelectedFile());
FileWriter writer = null;
try {
writer = new FileWriter(writepath);
try{
Iterator itr=locations.iterator();
while(itr.hasNext())
{
String pointLocation=(String) itr.next();
System.out.println(pointLocation);
String latLongs[] = getLatLongPositions( pointLocation);
writer.append(pointLocation);
writer.append(',');
writer.append(latLongs[0]);
writer.append(',');
writer.append(latLongs[1]);
writer.append('\n');
writer.flush();
System.out.println("Your excel file has been generated for " + pointLocation);
}
Object[] options = { "OK" };
int input = JOptionPane.showOptionDialog(null, "Excel generation is
completed. Click OK to continue", "",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
if(input == JOptionPane.OK_OPTION)
{
System.out.println("AASHIMA");
}
}
catch(Exception e1){
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
`
public static String[] getLatLongPositions(String address) throws Exception
{
int responseCode = 0;
String api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" +
URLEncoder.encode(address, "UTF-8") + "&sensor=true";
System.out.println("URL : "+api);
URL url = new URL(api);
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.connect();
responseCode = httpConnection.getResponseCode();
if(responseCode == 200)
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(httpConnection.getInputStream());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/GeocodeResponse/status");
String status = (String)expr.evaluate(document, XPathConstants.STRING);
if(status.equals("OK"))
{
expr = xpath.compile("//geometry/location/lat");
String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
expr = xpath.compile("//geometry/location/lng");
String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
return new String[] {latitude, longitude};
}
else
{
throw new Exception("Error from the API - response status: "+status);
}
}
return null;
}
控制台上的输出是这样的 `古瓦哈提阿萨姆邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Guwahati+Assam+india&sensor=true 您的excel文件已为Guwahati Assam印度生成 Muzaffarpur Bihar印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Muzaffarpur+Bihar+india&sensor=true 您的excel文件已为Muzaffarpur Bihar印度生成 Forbesganj Bihar印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Forbesganj+Bihar+india&sensor=true 您的excel文件已为Forbesganj Bihar印度生成 Ambikapur Chhattisgharh印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Ambikapur+Chhattisgharh+india&sensor=true 您的excel文件已为Ambikapur Chhattisgharh india生成 Raipur Chhattisgharh印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Raipur+Chhattisgharh+india&sensor=true 您的excel文件已为Raipur Chhattisgharh印度生成 苏拉特古吉拉特邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Surat+Gujarat+india&sensor=true 您的excel文件已为Surat Gujarat印度生成 瓦多达拉古吉拉特邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Vadodara+Gujarat+india&sensor=true 您的excel文件已为Vadodara Gujarat印度生成 拉杰科特古吉拉特邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Rajkot+Gujarat+india&sensor=true 您的excel文件已为Rajkot Gujarat印度生成 Panchkula哈里亚纳邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Panchkula+Haryana+india&sensor=true 您的excel文件已为Panchkula Haryana india生成 Deogharh Jharkhand印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Deogharh+Jharkhand+india&sensor=true 您的excel文件已为Deogharh Jharkhand印度生成 贾姆谢德布尔贾坎德邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Jamshedpur+Jharkhand+india&sensor=true 您的excel文件已为Jamshedpur Jharkhand印度生成 Hosur卡纳塔克邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Hosur+karnataka+india&sensor=true 您的excel文件已为Hosur karnataka india生成 Belgaum卡纳塔克邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Belgaum+karnataka+india&sensor=true 您的excel文件已为Belgaum karnataka india生成 Gwalior Madhya Pradesh印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Gwalior+Madhya+Pradesh+india&sensor=true 您的excel文件已为Gwalior Madhya Pradesh印度生成 Narsimhapur Madhya Pradesh印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Narsimhapur+Madhya+Pradesh+india&sensor=true 您的excel文件已为Narsimhapur Madhya Pradesh印度生成 浦那马哈拉施特拉邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Pune+Maharashtra+india&sensor=true 您的excel文件已为Pune Maharashtra印度生成 Nanded Maharashtra印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Nanded+Maharashtra+india&sensor=true 您的excel文件已为Nanded Maharashtra india生成 那格浦尔马哈拉施特拉邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Nagpur+Maharashtra+india&sensor=true 您的excel文件已为Nagpur Maharashtra india生成 Kolhapur马哈拉施特拉邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Kolhapur+Maharashtra+india&sensor=true 您的excel文件已为Kolhapur Maharashtra印度生成 Sangali Maharashtra印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Sangali+Maharashtra+india&sensor=true 您的excel文件已为Sangali Maharashtra印度生成 Navi Mumbai Maharashtra印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Navi+Mumbai+Maharashtra+india&sensor=true 您的excel文件已为Navi Mumbai Maharashtra印度生成 Ri Bhoi Meghalaya印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Ri+Bhoi+Meghalaya+india&sensor=true 您的Excel文件已经为Ri Bhoi Meghalaya印度生成 Sambalpur Orissa印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Sambalpur+Orissa+india&sensor=true 您的excel文件已为Sambalpur Orissa印度生成 春妮旁遮普邦印度 网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Chunni+Punjab+india&sensor=true
这里停止并且没有进展。此外,控件返回到UI,按钮再次达到未按下状态。
我发现了问题,请建议一个解决方案,以便我能得到理想的结果
答案 0 :(得分:0)
我已经解决了这个问题。问题是由于OVER_QUERY_LIMIT,所以我检查了它的状态,并在一些超时后递归调用该函数。这是代码。
public static String[] getLatLongPositions(String address) throws Exception
{
int responseCode = 0;
String api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" +
URLEncoder.encode(address, "UTF-8") + "&sensor=true";
System.out.println("URL : "+api);
URL url = new URL(api);
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.connect();
responseCode = httpConnection.getResponseCode();
System.out.println("The response code is: " +responseCode);
if(responseCode == 200)
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(httpConnection.getInputStream());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/GeocodeResponse/status");
String status = (String)expr.evaluate(document, XPathConstants.STRING);
System.out.println("the status is " +status);
if(status.equals("OK"))
{
expr = xpath.compile("//geometry/location/lat");
String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
expr = xpath.compile("//geometry/location/lng");
String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
return new String[] {latitude, longitude};
}
if (status.equals("OVER_QUERY_LIMIT")) {
TimeUnit.SECONDS.sleep(20);
return( getLatLongPositions(address));
}
else
{
throw new Exception("Error from the API - response status: "+status);
}
}
return null;
}`