无法在AsyncTask中执行AsyncTask

时间:2015-02-09 11:32:22

标签: android android-asynctask

我在代码中使用AsyncTask来获取:

  1. 位置

  2. 该位置的纬度和经度

  3. 每当我在execute()上调用AsyncTask方法时,都会收到以下错误。

    错误:

    java.lang.IllegalStateException: Cannot execute task: the task is already running.
    

    类别:

    public class GetPlaces extends AsyncTask<String, String, String> {
    
    
    
            private Context context;
            private GetPlaces getPlaces;
    
            private AutoCompleteTextView autoCompleteTextView;
    
            private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
            private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
            private static final String OUT_JSON = "/json";
    
            private static final String API_KEY = "key=My key";
    
            private String INPUT = "INPUT";
    
            private HttpURLConnection urlConnection = null;
            private StringBuilder stringBuilder = null;
            private URL url;
            private InputStream iStream = null;
    
            private JSONObject jsonObject;
            private JSONArray jsonArray;
    
            private String description = "";
    
            private GetLatLong getLatLong;
    
    
            public GetPlaces(Context context, String INPUT, AutoCompleteTextView autoCompleteTextView) {
                this.context = context;
                this.INPUT = INPUT;
                this.autoCompleteTextView = autoCompleteTextView;
                this.execute(INPUT);
    
            }
    
            @Override
            protected String doInBackground(String... params) {
    
                String response = "";
    
                try {
                    INPUT = "input=" + URLEncoder.encode(params[0], "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
    
                }
    
                String types = "types=geocode";
    
                // Sensor enabled
                String sensor = "sensor=false";
    
                // Building the parameters to the web service
                String parameters = INPUT + "&" + types + "&" + API_KEY + "&" + sensor;
    
                stringBuilder = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
                stringBuilder.append("?" + parameters);
    
    
                //connections is established here
                try {
                    url = new URL(stringBuilder.toString());
    
                    urlConnection = (HttpURLConnection) url.openConnection();
    
                    // Reading data from url
                    iStream = urlConnection.getInputStream();
    
                    BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
    
                    StringBuffer sb = new StringBuffer();
    
                    String line = "";
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                    }
    
                    response = sb.toString();
                    Log.e("Response", response);
    
                    br.close();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        iStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    urlConnection.disconnect();
                }
    
                return response;
            }
    
            @Override
            protected void onPostExecute(String s) {
    
    
    
    
                getLatLong = new GetLatLong(context, s);
                getLatLong.execute(s);   // App crashes here
    
    
            }
    

    Logcat显示它在onPostExecute()方法的第二行崩溃。 (注释)。

1 个答案:

答案 0 :(得分:0)

这将导致无限循环。您可以查看this帖子以获得所需的结果。