I've been trying to implement asynctask to make about 30 http requests to find the distance between two locations using a JSON object and the distance matrix api. The code I've written works when called from the main UI thread, but when I try to run it from the Async Task and save the distances to an array I just end up with an array full of null values. Any advice? (Note: This code was initially written by someone else at my work, and I've merely copy pasted it and changed a few lines to work with my app. So, there may be some unnecessary bits that I'm unaware of. Feel free to point them out)
class DistanceFinder extends AsyncTask<String[], Void, String[]>
{
@Override
protected String[] doInBackground(String[]... locations)
{
String baseURL="https://maps.googleapis.com/maps/api/distancematrix/json?origins=";
String[] distances = new String[locations[1].length];
for(int i = 1;i<locations.length;i++)
{
String url = baseURL + locations[0][0].replace(" ","+") + "&destinations=" + locations[1][i].replace(' ', '+') + "&sensor=true&units=imperial";
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = "";
boolean internet;
try
{
response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
internet=true;
}
else
{
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
internet=false;
Toast.makeText(getApplicationContext(), "Please connect to internet", Toast.LENGTH_LONG).show();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
internet=false;
Toast.makeText(getApplicationContext(),"Please connect to internet", Toast.LENGTH_LONG).show();
}
if(internet){
try
{
JSONObject jsonObj = new JSONObject(responseString);
JSONArray rows = jsonObj.getJSONArray("rows");
JSONObject inRows=rows.getJSONObject(0);
JSONArray elements = inRows.getJSONArray("elements");
JSONObject inElements=elements.getJSONObject(0);
JSONObject distance= inElements.getJSONObject("distance");
distances[i] = distance.getString("text");
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return distances;
}
@Override
protected void onPostExecute(String[] result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
distancesList = result;
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
}
}
Your problem is with you for loop
for(int i = 1;i<locations.length;i++)
First, you should start from 0, unless your first cell doesn't store a String
you wish you check the distance to.
Second, your for loop should be
for(int i = 0;i<locations[0].length;i++)
Right now you're checking cells [1][0]
and that's it, because the loop ends.
I tested it with manually entered locations and it works.
Also, just to make things easier for you to debug, You should really get used to using Log.d()
. It really helps figuring out errors. I used it in your code and saw that the loop only gets executed once.
Good luck
Ps, as mentioned in one of the comments, remove the onPreExecute()
. You don't use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.