Asynctask#1 Android错误

时间:2013-11-05 22:30:52

标签: java android mysql google-maps

新版本1.1

你好。我试图按照你的建议,现在我正在尝试重做代码。 我添加了这部分代码,以便在主线程上使用GoogleMaps标记。

问题是,当我调用Ubic()函数时,我收到此错误:

http://i.imgur.com/QD7JZjRh.jpg

http://i.imgur.com/xvzJ9ckh.jpg

这是我添加的代码错误是由String Latlon [] [] = com.Ubic()引起的;

public class BuscarContrincantes extends FragmentActivity implements
    LocationListener {

GoogleMap googlemapa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buscarcontrincante);

    SupportMapFragment maps = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    googlemapa = maps.getMap();
    googlemapa.setMyLocationEnabled(true);
    googlemapa.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    googlemapa.getUiSettings().setCompassEnabled(true);
    googlemapa.getUiSettings().setZoomControlsEnabled(true);
    googlemapa.getUiSettings().setAllGesturesEnabled(true);

    double latitud;
    double longitud;
    String nombre;

    Datos com = new Datos();

    String latlon[][] = com.Ubic(); //HERE IS THE PROBLEM!

    for (int i = 0; i < latlon.length - 1; i++) {
        latitud = Double.parseDouble(latlon[i][0]);
        longitud = Double.parseDouble(latlon[i][3]);
        nombre = (latlon[i][2]);

        LatLng posicion = new LatLng(latitud, longitud);

        googlemapa.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.icono_canchas))
                .position(posicion).title(nombre));


}

}

VERSION 1.0

我正在为我的大学做的项目遇到问题。

实际上,我正试图在地图上设置标记(使用谷歌地图),他的信息在MySQL数据库上。

代码是:

BuscarContrincantes.java

public class BuscarContrincantes extends FragmentActivity implements
    LocationListener {

GoogleMap googlemapa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buscarcontrincante);

    SupportMapFragment maps = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    googlemapa = maps.getMap();
    googlemapa.setMyLocationEnabled(true);
    googlemapa.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    googlemapa.getUiSettings().setCompassEnabled(true);
    googlemapa.getUiSettings().setZoomControlsEnabled(true);
    googlemapa.getUiSettings().setAllGesturesEnabled(true);

    new RetreiveFeedTask().execute();

}

class RetreiveFeedTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... args) {
        double lat;
        double lon;
        String nombre;

        Datos com = new Datos(); // File Datos.java
        String latlon[][] = com.Ubic();

        for (int i = 0; i < latlon.length - 1; i++) {
            lat = Double.parseDouble(latlon[i][0]);
            lon = Double.parseDouble(latlon[i][4]);
            nombre = (latlon[i][2]);

            LatLng pos3 = new LatLng(lat, lon);

            googlemapa.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.icono_canchas))
                    .position(pos3).title(nombre));

        }
        return null;

    }

}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}}

Datos.java

public class Datos {
public String[][] Ubic() {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "http://MY.IP/FOLDER/QUERY.php");

    try {

        HttpResponse response = httpclient.execute(httppost);
        String jsonResult = inputStreamToString(
                response.getEntity().getContent()).toString();
        JSONObject object = new JSONObject(jsonResult);


        JSONArray arr = object.getJSONArray("products");
        String[] lat = new String[arr.length()+1];
        String[] lon = new String[arr.length()+1];
        String[] nombre = new String[arr.length() + 1];

        String[][] latlon = new String[arr.length() + 1][5];

        for (int i = 0; i < arr.length(); i++) {
            lat[i] = arr.getJSONObject(i).getString("latitude");
            lon[i] = arr.getJSONObject(i).getString("longitude");
            name[i] = arr.getJSONObject(i).getString("name");
            latlon[i][0] = lat[i];
            latlon[i][6] = lon[i];
            latlon[i][2] = name[i];
        }
        return latlon;

    } catch (JSONException e) {

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }
    return null;

}

private static String parse(String string) {
    return null;

}

private StringBuilder inputStreamToString(InputStream is) {
    String rLine = "";
    StringBuilder answer = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    try {
        while ((rLine = rd.readLine()) != null) {
            answer.append(rLine);
        }
    } catch (IOException e) {

    }
    return answer;
}}

此代码试图获取MySQL数据库上的对象的纬度,经度和名称,并且必须将此对象放在Map上,但我得到的是AsynkTask#1错误,我不知道为什么会这样正在发生。 你能帮我吗?我对此代码感到非常生气,我不知道为什么这不起作用。

P.S:抱歉我的英语不好。我来自智利:P

1 个答案:

答案 0 :(得分:0)

您无法以doInBackground方式访问GoogleMap实例。只能从主线程中读取和修改GoogleMap。只是HTTP请求应该是后台任务。

您需要修改doInBackground以返回坐标列表,并以onPostExecute方法(主线程)实施GoogleMap更新。

以下是基于版本1.1的代码:

GoogleMap googlemapa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buscarcontrincante);

    SupportMapFragment maps = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    googlemapa = maps.getMap();
    googlemapa.setMyLocationEnabled(true);
    googlemapa.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    googlemapa.getUiSettings().setCompassEnabled(true);
    googlemapa.getUiSettings().setZoomControlsEnabled(true);
    googlemapa.getUiSettings().setAllGesturesEnabled(true);

    new RetreiveFeedTask().execute();
}

class RetreiveFeedTask extends AsyncTask<Void, Void, String[][]> {

    @Override
    protected String[][] doInBackground(Void... args) {
        // Execute the HTTP request on background task
        Datos com = new Datos(); // File Datos.java
        return com.Ubic();
    }

    @Override
    protected void onPostExecute(String[][] latlon) {
        double latitud;
        double longitud;
        String nombre;

        // Update the UI
        for (int i = 0; i < latlon.length - 1; i++) {
            latitud = Double.parseDouble(latlon[i][0]);
            longitud = Double.parseDouble(latlon[i][3]);
            nombre = (latlon[i][2]);

            LatLng posicion = new LatLng(latitud, longitud);

            googlemapa.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.icono_canchas)).position(posicion).title(nombre));

        }
    }
}