为了进行研究,我们必须开发一款基于位置的 Android 游戏。目前我们使用OSMDroid来显示地图。玩家必须收集资源(如木材、石头……)。这些资源当前以硬编码的经/纬度存储在我们的后端,并将通过 setMarker 添加到当前地图上。
为了在全球范围内提供此游戏,我们希望根据“真实”世界动态设置资源。因此,我们需要 OSM 的不同层(如森林、海洋……)来自动设置我们的资源,而无需询问我们的后端。
经过几个小时的谷歌搜索后,我发现 Overpass API 似乎可以帮助我实现此功能。但我找不到任何在 Android 中使用 Overpass API 的教程。我尝试了一些东西,但我不明白......所以我需要你的帮助,请给我一个例子或解释如何实现这个:/
这是我当前的代码,但我认为这是不正确的..
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();
:
W/System.err(3958): java.io.FileNotFoundException:http://overpass-api.de/api/interpreter http://overpass-api.de/api/interpreterW/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/interpreter
返回一个400 Bad Request
回复。
您想要做的是一个 POST 请求http://overpass-api.de/api/interpreter
. 一个例子 http://overpass-turbo.eu/ of form-data
传递给这个 API 的是:
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>'
要了解 API 的工作原理,您应该使用浏览器检查单击时对 API 进行的 HTTP 查询Run
in the 我指出的例子 http://overpass-turbo.eu/.
EDIT
你可以找到很多例子,比如this one https://stackoverflow.com/questions/2938502/sending-post-data-in-android显示了如何在 Android 中使用 HTTP 发布数据。你必须添加data
as a keyXML 查询字符串作为value在使用的值对容器中,例如:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", _The_XML_Query_String_));
坚持链接示例 https://stackoverflow.com/questions/2938502/sending-post-data-in-android剩下的你应该没问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)