如何自动实现地图标记?错误的连接或错误的代码?

How can I automatically implement map markers? Wrong connection or wrong code?

我很难从 MySQL 数据库接收 google 地图标记。 我有这个问题 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'int java.util.ArrayList.size()'。

我的部分代码:

ArrayList<HashMap<String, String>> location = null;
    try {

        JSONArray data = new JSONArray(RetrieveTask.class);

        location = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++) {
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("id", c.getString("id"));
            map.put("lat", c.getString("lat"));
            map.put("lng", c.getString("lng"));
            location.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < location.size/*here it shows the problem - size*/(); i++) {
        String id = location.get(i).get("id");
        double lat = Double.parseDouble(location.get(i).get("lat"));
        double lng = Double.parseDouble(location.get(i).get("lng"));
        LatLng latLng = new LatLng(lat, lng);
        MarkerOptions marker = new MarkerOptions().position(latLng).title(id);
        googleMap.addMarker(marker);
    }

以及连接互联网的代码:

private class RetrieveTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... voids) {
        String strUrl = "http://12345.php";
        URL url = null;
        StringBuffer sb = new StringBuffer();

        try {
            url = new URL(strUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream iStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
            String line = "";

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

            reader.close();
            iStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

}

我不知道我的代码有什么问题,我尝试了不同的方法,但仍然遇到同样的错误。 有没有人遇到过同样的问题,你是怎么解决的?

可能是数据库故障,因为不是远程的,我用的是localhost? JSON 响应正常,它 returns 预期值。

你肯定得到了 NPE,因为你捕获了 JSONException

catch (JSONException e) {
        e.printStackTrace();
}

因为这条线你得到了它

JSONArray data = new JSONArray(RetrieveTask.class);

您没有使用此行从互联网获取数据。您只需尝试从 class 对象创建 JSONArray

您必须在 AsyncTask

中使用方法 onPostExecute
 protected void onPostExecute(String result) {
     //call here method of Activity or parse your JSONArray
     JSONArray data = new JSONArray(result);
     //your other code
 }

我认为您没有按照正确的程序从本地主机检索数据。您首先需要覆盖 AsyncTask 的 onPostExecute() 方法,然后您应该在 onPostExecute() 方法中从本地主机检索结果数据。

你的代码在这一行崩溃

JSONArray data = new JSONArray(RetrieveTask.class);

因此位置尚未初始化。因此,当您尝试在下一个 for 循环中访问位置时,它会抛出 NullPointerException。

您的代码应该是这样的:

private class RetrieveTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... voids) {
    String strUrl = "http://12345.php";
    URL url = null;
    StringBuffer sb = new StringBuffer();

    try {
        url = new URL(strUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream iStream = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
        String line = "";

        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }

        reader.close();
        iStream.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return sb.toString();
}

@Override
protected String onPostExecute(String result){
ArrayList<HashMap<String, String>> location = null;
try {

    JSONArray data = new JSONArray(result);

    location = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map;

    for(int i = 0; i < data.length(); i++) {
        JSONObject c = data.getJSONObject(i);

        map = new HashMap<String, String>();
        map.put("id", c.getString("id"));
        map.put("lat", c.getString("lat"));
        map.put("lng", c.getString("lng"));
        location.add(map);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

//the if loop is to check if location is initialised above
if(location!=null){ 
        for (int i = 0; i < location.size/*here it shows the problem - size*/(); i++) {
            String id = location.get(i).get("id");
            double lat = Double.parseDouble(location.get(i).get("lat"));
            double lng = Double.parseDouble(location.get(i).get("lng"));
            LatLng latLng = new LatLng(lat, lng);
            MarkerOptions marker = new MarkerOptions().position(latLng).title(id);
            googleMap.addMarker(marker);
        }
    }
}

}

那么你可以这样执行AsyncTask:

new RetrieveTask().execute();

这里有一个 AsyncTask 的例子,它会帮助你理解它的正确流程。

You can skip to Step no. 7 to know about the Android setup for connecting to localhost