-2

I'm trying to get Latitude, Longitude and Name from a SQL Query on Java.

The problem is, when i call the Ubic() function, i get this error:

https://i.stack.imgur.com/Bkudh.jpg

https://i.stack.imgur.com/WUTcF.jpg

This is the code that i added an the error is caused by 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));


}

}

The file Datos.java is what i called from the Main Thread.

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

How can i solve this?

Kara
  • 6,115
  • 16
  • 50
  • 57

1 Answers1

1

NetworkOnMainThreadException: This exception is thrown when an application attempts to perform a networking operation (HTTP request in your case) on its main thread.

You should call com.Ubic(); method on asynctask then only above code will work.

Refer here http://android-developers.blogspot.in/2009/05/painless-threading.html

You will find better soltion here AsyncTask Android example

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

Community
  • 1
  • 1
Jayasagar
  • 2,046
  • 19
  • 22
  • I tried to call com.Ubic(); method on Asynctask, but it doesn't work. Can you me help with the code that could work? – user2958238 Nov 08 '13 at 08:33