I am trying to run an asynchronous task after getting gps coordinates from my map routine in an Android MainActivity. The asynchronous task is to do an httpurlconnection to get data from my server based on the gps coordinates. However, the asynchronous task GetRestaurant() never gets called while observing in my debug process. I should mention while in debugger the looper routine is called over and over. In debug I noticed that the program executes the GetRestaurant instruction but after that nothing happens. The class is never reached. The map does get displayed and that's about it. Can anyone tell from the code below as to why my asynchronous task class GetRestaurant never gets executed?
locationManager.requestLocationUpdates(provider, 3600, 1000, new LocationListener() {
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onLocationChanged(Location loc) {
if(loc != null) {
lat = (float) (loc.getLatitude());
lng = (float) (loc.getLongitude());
String latLongString = "Lat:" + lat + "\nLong:" + lng;
myLocationText.setText("Your current position is:\n" + latLongString);
int latit = (int) (loc.getLatitude() * 1E6);
int longit = (int) (loc.getLongitude() * 1E6);
point = new GeoPoint(latit, longit);
mapController.animateTo(point);
mapController.setCenter(point);
mapController.setZoom(15);
String addr=getAddress(point);
TextView addressView = (TextView) findViewById(R.id.address_view);
addressView.setText(addr);
MyOverlay mapOverlay = new MyOverlay();
List<Overlay> overlayList=mapView.getOverlays();
overlayList.clear();
overlayList.add(mapOverlay);
new GetRestaurant().execute();
}
}
});
}
class GetRestaurant extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void...parameters ) {
// TODO Auto-generated method stub
String thislat = ((String.valueOf(lat)));
String thislng = ((String.valueOf(lng)));
String[] result = null;
URL url;
try {
String query = "lat=" + thislat + "&lng=" + thislng + "&uid=&vegkey=1";
url = new URL("http://10.0.2.2:8000/xxxxxxx/webservice/frontpage1.php?" + query);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
int x=0;
while ((line = br.readLine()) != null) {
// sb.(line);
result[x] = line;
x++;
}
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
System.out.println(e.toString());
}
return result;
}
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(String[] result) {
restaurants = result;
final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(null, android.R.layout.simple_list_item_1, restaurants);
restaurant_list.setAdapter(arrayAdpt);
mapView.postInvalidate();
}
}
Here is my errorlog: I noticed this error: "Can't create handler inside thread that has not called Looper.prepare()." That sounds suspicious.
03-08 23:53:27.846: E/AndroidRuntime(570): FATAL EXCEPTION: AsyncTask #1
03-08 23:53:27.846: E/AndroidRuntime(570): java.lang.RuntimeException: An error occured while executing doInBackground()
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.lang.Thread.run(Thread.java:856)
03-08 23:53:27.846: E/AndroidRuntime(570): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.Handler.<init>(Handler.java:197)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.Handler.<init>(Handler.java:111)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.widget.Toast$TN.<init>(Toast.java:324)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.widget.Toast.<init>(Toast.java:91)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.widget.Toast.makeText(Toast.java:238)
03-08 23:53:27.846: E/AndroidRuntime(570): at com.example.herb4.MainActivity$GetRestaurant.doInBackground(MainActivity.java:159)
03-08 23:53:27.846: E/AndroidRuntime(570): at com.example.herb4.MainActivity$GetRestaurant.doInBackground(MainActivity.java:1)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-08 23:53:27.846: E/AndroidRuntime(570): ... 4 more
Ok. I changed the code to read 'return (String[]) result.toArray() at the end of doInBackground. Now I get the following error: '03-09 03:24:18.796: E/AndroidRuntime(4726): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]'. I think the error occurred in onPostExecute which has the following code"
@Override
protected void onPostExecute(String[] result) {
// restaurants = result;
final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, result);
restaurant_list.setAdapter(arrayAdpt);
mapView.postInvalidate();
}
This would be a lot easier if I could follow the program in the debugger but it quits before I get to the problem area.