-2

我正在尝试从 Java 上的 SQL 查询中获取纬度、经度和名称。

问题是,当我调用 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));


}

}

文件 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;
}}

我该如何解决这个问题?

4

1 回答 1

1

NetworkOnMainThreadException:当应用程序尝试在其主线程上执行网络操作(在您的情况下为 HTTP 请求)时,将引发此异常。

您应该com.Ubic();在 asynctask 上调用方法,然后只有上面的代码才能工作。

参考这里http://android-developers.blogspot.in/2009/05/painless-threading.html

你会在这里找到更好的解决方案 AsyncTask Android 示例

http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

于 2013-11-08T03:54:14.607 回答