立交桥API Android的例子? [英] Overpass API Android Example?
问题描述
有关研究中,我们要开发一个Android游戏,这是基于位置。目前我们使用OSMDroid显示地图。有资源(如木材,石材,...),该玩家有收集。这些资源当前存储在我们与硬$ C $光盘经度/纬度的后端,并将与setMarker被添加到当前地图。
在全球范围内提供了本场比赛,我们要立足于现实世界动态设置的资源。因此,我们需要不同的层从OSM(如森林,海洋,...),自动设置我们的资源不问我们的后端。
几个小时与谷歌搜索后,我发现该立交桥API似乎帮助我实现这个功能。但是,我找不到任何教程在Android中使用API立交桥。我尝试了一些东西,但我不明白这一点。所以我需要你的帮助,请给我一个例子或解释如何实现这一点:/
这是我目前的code,但我不认为这是正确的..
网址URL =新的URL(http://overpass-api.de/api/inter$p$pter);
HttpURLConnection类的URLConnection =(HttpURLConnection类)url.openConnection();
urlConnection.setRequestMethod(GET);
urlConnection.setDoOutput(真);
urlConnection.connect();
为InputStream的InputStream = urlConnection.getInputStream();
inputStream.close();
以下异常将在的InputStream的InputStream = urlConnection.getInputStream()抛出;
:
W / System.err的(3958):java.io.FileNotFoundException:的http:/ /overpass-api.de/api/inter$p$pter
W / System.err的(3958):在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
W / System.err的(3958):在de.htw.berlin.games.based.location.gui.MapActivity $ test.doInBackground(MapActivity.java:536)
W / System.err的(3958):在de.htw.berlin.games.based.location.gui.MapActivity $ test.doInBackground(MapActivity.java:1)
W / System.err的(3958):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
W / System.err的(3958):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
W / System.err的(3958):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
W / System.err的(3958):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)
W / System.err的(3958):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W / System.err的(3958):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
W / System.err的(3958):在java.lang.Thread.run(Thread.java:856)
感谢您的所有有用的回复:)
你得到这个异常被抛出,因为一个HTTP GET调用 http://overpass-api.de/api/inter preTER
返回 400错误的请求
响应。
你想要做什么是一个POST请求 http://overpass-api.de/api/inter$p$pter
。 表单数据的例子
来传递给该API是:
数据='< XML版本=1.0编码=UTF-8>< OSM-脚本><! -
这是一个例子立交桥查询。
尝试一下由pressing上面的运行按钮!
你可以找到加载工具更多的例子。
- >
<查询类型=节点>
<拥有千伏K =美化市容V =drinking_water/>
< BBOX查询S =41.88659196260802W =12.488558292388916N =41.89248629819397E =12.51119613647461/><! - 这是自动完成的
当前地图视图坐标.-->
< /查询>
<打印/>< / OSM-脚本>'
要了解如何的API是工作,你应该检查,使用你的浏览器,什么HTTP查询点击时的API的运行
中的例如我指出。
修改
您可以找到大量的如这个,说明如何发布数据使用Android中的HTTP。你必须添加数据
为键的与XML查询字符串作为的值的在使用的值对容器中,诸如:
列表<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(1);
nameValuePairs.add(新BasicNameValuePair(数据,_The_XML_Query_String_));
坚持href=\"http://stackoverflow.com/questions/2938502/sending-post-data-in-android\">联例如,在休息,你HOULD被罚款的 for study we have to develop an Android game which is location based. Currently we use OSMDroid to show the map. There are resources (like wood, stone, ...) which the player has to collect. These resources are currently stored in our backend with hardcoded long/lat and will be added with setMarker onto the current map.
To provide this game globally, we want to set the resources dynamically based on the "real" world. So we need different layers from OSM (like forest, sea, ..) to set our resources automatically without asking our backend.
After some hours searching with google I found out that the Overpass API seems to help me implementing this functionality. But I can't find any tutorial for using Overpass API in Android. I tried some things but I don't get it... So I need your help, please give me an example or explanation how to implement this :/ This is my current code, but I don't think that this is correct..
Following exception will be thrown at W/System.err(3958): java.io.FileNotFoundException: http://overpass-api.de/api/interpreter
W/System.err(3958): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
W/System.err(3958): at de.htw.berlin.games.based.location.gui.MapActivity$test.doInBackground(MapActivity.java:536)
W/System.err(3958): at de.htw.berlin.games.based.location.gui.MapActivity$test.doInBackground(MapActivity.java:1)
W/System.err(3958): at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(3958): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(3958): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err(3958): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(3958): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err(3958): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err(3958): at java.lang.Thread.run(Thread.java:856) Thanks for all helpful replies :) This exception you're getting is thrown because an HTTP GET call to What you want to do is a POST request to To find out how the API is working you should check, using your browser, what HTTP query is made to the API when clicking on EDIT You can find plenty of examples like this one that shows how to post data using HTTP in Android. You'll have to add Stick to the linked example for the rest and you hould be fine. 这篇关于立交桥API Android的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!URL url = new URL("http://overpass-api.de/api/interpreter");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
inputStream.close();
InputStream inputStream = urlConnection.getInputStream();
:http://overpass-api.de/api/interpreter
returns a 400 Bad Request
response.http://overpass-api.de/api/interpreter
. An example of form-data
to pass to this API is : data='<?xml version="1.0" encoding="UTF-8"?><osm-script><!--
This is an example Overpass query.
Try it out by pressing the Run button above!
You can find more examples with the Load tool.
-->
<query type="node">
<has-kv k="amenity" v="drinking_water"/>
<bbox-query s="41.88659196260802" w="12.488558292388916" n="41.89248629819397" e="12.51119613647461"/><!--this is auto-completed with the
current map view coordinates.-->
</query>
<print/></osm-script>'
Run
in the example I pointed out.data
as a key and the XML query string as a value in the used value pair container, such as :List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", _The_XML_Query_String_));