从JSON、Android向映射添加标记
我试图在从JSON获取数据后自动将标记添加到地图中,api中的位置如下:从JSON、Android向映射添加标记,android,json,google-maps,Android,Json,Google Maps,我试图在从JSON获取数据后自动将标记添加到地图中,api中的位置如下: @Override protected Void doInBackground(Void... params) { HttpHandler sh2 = new HttpHandler(); final String jsonStrOportunidades = sh2.makeServiceCall(urlOportunidades); Log.e(TAG, "Res
@Override
protected Void doInBackground(Void... params) {
HttpHandler sh2 = new HttpHandler();
final String jsonStrOportunidades = sh2.makeServiceCall(urlOportunidades);
Log.e(TAG, "Response from URL: " + jsonStrOportunidades);
if (jsonStrOportunidades != null) {
try {
JSONArray array = new JSONArray(jsonStrOportunidades);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String Designacao = jsonObject.getString("Designacao");
String Coord_LAT = jsonObject.getString("Coord_LAT");
String Coord_LONG = jsonObject.getString("Coord_LONG");
HashMap<String, String> oportunidades = new HashMap<>();
oportunidades.put("Designacao", Designacao);
oportunidades.put("Coord_LAT", Coord_LAT);
oportunidades.put("Coord_LONG", Coord_LONG);
double lat1 = Double.parseDouble(Coord_LAT);
double lng1 = Double.parseDouble(Coord_LONG);
mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));
listaOportunidades.add(oportunidades);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Json parsin error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
return null;
}
@覆盖
受保护的Void doInBackground(Void…参数){
HttpHandler sh2=新的HttpHandler();
最终字符串jsonStrOportunidades=sh2.makeServiceCall(urlOportunidades);
Log.e(标签,“来自URL的响应:+JSONSTROTORTUNIDADES”);
如果(JSONSTROTORTUNIDADES!=null){
试一试{
JSONArray数组=新的JSONArray(JSONSTROTORTUNIDADS);
对于(int i=0;i
“mMap.addMarker”不起作用,甚至可以从那里创建标记吗?尝试从
onPostExecute
设置标记doInBackground
不是更新UI组件的地方
@Override
protected String doInBackground(Void... params) {
HttpHandler sh2 = new HttpHandler();
final String jsonStrOportunidades = sh2.makeServiceCall(urlOportunidades);
Log.e(TAG, "Response from URL: " + jsonStrOportunidades);
return jsonStrOportunidades;
}
@Override
protected void onPostExecute(String jsonStrOportunidades){
if (jsonStrOportunidades != null) {
try {
JSONArray array = new JSONArray(jsonStrOportunidades);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String Designacao = jsonObject.getString("Designacao");
String Coord_LAT = jsonObject.getString("Coord_LAT");
String Coord_LONG = jsonObject.getString("Coord_LONG");
HashMap<String, String> oportunidades = new HashMap<>();
oportunidades.put("Designacao", Designacao);
oportunidades.put("Coord_LAT", Coord_LAT);
oportunidades.put("Coord_LONG", Coord_LONG);
double lat1 = Double.parseDouble(Coord_LAT);
double lng1 = Double.parseDouble(Coord_LONG);
mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));
listaOportunidades.add(oportunidades);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
Toast.makeText(getApplicationContext(), "Json parsin error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@覆盖
受保护字符串doInBackground(无效…参数){
HttpHandler sh2=新的HttpHandler();
最终字符串jsonStrOportunidades=sh2.makeServiceCall(urlOportunidades);
Log.e(标签,“来自URL的响应:+JSONSTROTORTUNIDADES”);
返回JSONSTROTORTUNIDADES;
}
@凌驾
受保护的void onPostExecute(字符串JSONSTROTORTUNIDADES){
如果(JSONSTROTORTUNIDADES!=null){
试一试{
JSONArray数组=新的JSONArray(JSONSTROTORTUNIDADS);
对于(int i=0;i
另外,您需要像这样更改AsyncTask类
YourAsyncTaskClass extends AsyncTask<String, Void, String>
YourAsyncTaskClass扩展了AsyncTask
您遇到了什么异常或错误?@rafsanahmad007致命异常:AsyncTask#2@alb请回答您的问题,logcatonPostExecute中的完整异常已在UI线程上运行。您不需要在那里修改它,只需将“受保护的void doInBackground(void…params)”更改为“公共字符串doInBackground(Object…params)”。谢谢@板球-对!我真傻。谢谢你指出这一点。还有一个新手犯的错误。在Atom中键入代码,自然不会出现任何返回类型错误,因此我忽略了它。谢谢你指出这一点。我根据你们的输入更新了我的代码。