我正在用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;
}控制台上的输出是这个‘Guwahati Assam印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Guwahati+Assam+india&sensor=true您的excel文件已经为Guwahati Assam印度Muzaffarpur Bihar印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Muzaffarpur+Bihar+india&sensor=true您的excel文件已经为Muzaffarpur比哈尔邦印度福贝斯甘杰比哈尔印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Forbesganj+Bihar+india&sensor=true您的excel文件已经为福贝斯甘吉比哈尔印度Ambikapur恰蒂斯加尔邦印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Ambikapur+Chhattisgharh+india&sensor=true您的excel文件已经为印度古吉拉特邦生成URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Raipur+Chhattisgharh+india&sensor=true您的excel文件已经为苏拉特古吉拉特邦印度生成URL:C7<您的excel文件已经为苏拉特古吉拉特邦印度Vadodara古吉拉邦印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Vadodara+Gujarat+india&sensor=true您的excel文件已为瓦多达拉古吉拉特邦印度Rajkot Gujarat印度URL生成:http://maps.googleapis.com/maps/api/geocode/xml?address=Rajkot+Gujarat+india&sensor=true您的excel文件已为拉吉古吉拉特邦印度生成Panchkula Haryana印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Panchkula+Haryana+india&sensor=true您的excel文件已为本奇库拉邦生成印度Deogharh恰尔肯德邦印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Deogharh+Jharkhand+india&sensor=true您的excel文件已为Deogharh恰尔肯德邦印度Jamshedpur恰尔肯德邦印度URL:http://maps.googleapis.com/maps/api/geocode/xml?address=Jamshedpur+Jharkhand+india&sensor=true您的excel文件已为印度霍苏尔卡纳塔克邦生成:http://maps.googleapis.com/maps/api/geocode/xml?address=Hosur+karnataka+india&sensor=true您的excel文件已为贝尔甘邦卡纳塔克邦印度生成:http://maps.googleapis.com/maps/api/geocode/xml?address=Belgaum+karnataka+india&sensor=true您的excel文件已为贝尔加姆卡纳塔克邦印度生成瓜利奥尔·中央邦印度网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Gwalior+Madhya+Pradesh+india&sensor=true您的excel文件已为瓜利尔邦生成,纳西姆哈普尔·中央邦印度网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Narsimhapur+Madhya+Pradesh+india&sensor=true您的excel文件已为那西哈普邦生成,普纳·马哈拉施特拉邦印度网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Pune+Maharashtra+india&sensor=true您的excel文件已为普纳·马哈拉施特拉邦生成,印度南德·马哈拉施特拉邦网址:http://maps.googleapis.com/maps/api/geocode/xml?address=Nanded+Maharashtra+india&sensor=true您的excel文件已为纳吉布·马哈拉施特拉邦印度生成:http://maps.googleapis.com/maps/api/geocode/xml?address=Nagpur+Maharashtra+india&sensor=true您的excel文件已为那格浦尔·马哈拉施特拉邦印度生成:http://maps.googleapis.com/maps/api/geocode/xml?address=Kolhapur+Maharashtra+india&sensor=true您的excel文件已为印度马哈拉施特拉邦生成:http://maps.googleapis.com/maps/api/geocode/xml?address=Sangali+Maharashtra+india&sensor=true您的excel文件已为印度桑加利马哈拉施特拉邦生成:http://maps.googleapis.com/maps/api/geocode/xml?address=Navi+Mumbai+Maharashtra+india&sensor=true您的excel文件已为纳维孟买马哈拉施特拉邦印度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,在UI中按钮再次达到未按下状态。
它完全是随机执行的。这是屏幕截图

响应代码是200,但我仍然面临同样的问题

我已经找到问题了,请给我一个解决方案,这样我才能得到想要的结果。

发布于 2017-09-26 18:22:43
我已经解决了这个问题。这个问题是由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;
}`https://stackoverflow.com/questions/46419241
复制相似问题