Asynctask无法完成其任务

时间:2014-02-13 16:58:28

标签: android android-asynctask

希望你的表现很好。 我在我的应用程序中使用Asynctask从服务器获取数据和少量图像,并将它们存储在数据库和sdCard上。 当我在三星Pocket Plus上测试时,一切顺利,当我在三星S3上测试时,Asynctask无法完成任务。 它只能从服务器下载一个图像,然后停止。 我不知道是什么问题,两个手机在测试时使用相同的互联网连接。

class RequestRestaurants extends AsyncTask <Void, Void, Void> {
     @Override
    protected void onPreExecute(){
          super.onPreExecute();
        }
     @Override
    protected Void doInBackground(Void... uri) {            
        myDatabaseHandler.openToWrite();
        try {
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setSoTimeout(params, 0);
            HttpClient httpClient = new DefaultHttpClient(params);
            //prepare the HTTP GET call 
            HttpGet httpget = new HttpGet("http://restaurants.bugs3.com/restaurants.php");
            //get the response entity
            HttpEntity entity = httpClient.execute(httpget).getEntity();
            if (entity != null) {
                //get the response content as a string
                String response = EntityUtils.toString(entity);
                //consume the entity
                entity.consumeContent();

                // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
                httpClient.getConnectionManager().shutdown();

                //return the JSON response
                JSONObject object = new JSONObject(response.trim());
                JSONArray jsonArray = object.getJSONArray("restaurants");
                if(jsonArray != null) {
                   for(int i = 0 ; i < jsonArray.length() ; i++) {
                       JSONObject object1 = (JSONObject) jsonArray.get(i); 
                       int server_restaurant_id = object1.getInt("server_restaurant_id");
                       String restaurant_name = object1.getString("restaurant_name");
                       String restaurant_hotline = object1.getString("restaurant_hotline");
                       String restaurant_image_name = object1.getString("restaurant_image_name");
                       String restaurant_image_link = object1.getString("restaurant_image_link");
                       String restaurant_image_path = object1.getString("restaurant_image_path");
                       try
                        {
                         URL url = new URL(restaurant_image_link);
                         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                         connection.setDoInput(true);
                         connection.connect();
                         InputStream input = connection.getInputStream();
                         Bitmap myBitmap = BitmapFactory.decodeStream(input);                             
                        try {
                                String filePath = sdIconStorageDir.toString() + "/" + restaurant_image_name + ".png";
                                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                                    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
                                //choose another format if PNG doesn't suit you
                                myBitmap.compress(CompressFormat.PNG, 10, bos);
                                    bos.flush();
                                    bos.close();
                            } catch (FileNotFoundException e) {
                                Log.w("TAG", "Error saving image file: " + e.getMessage());
                            } catch (IOException e) {
                                Log.w("TAG", "Error saving image file: " + e.getMessage());
                            }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                       myDatabaseHandler.insertRestaurantWithID(server_restaurant_id, restaurant_name, restaurant_hotline, restaurant_image_name, restaurant_image_link, restaurant_image_path);
                   }
                } 
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(final Void x) {
        myDatabaseHandler.openToRead();
        if(!myDatabaseHandler.checkDatabaseIsNull("restaurants"))
        {
            myDatabaseHandler.close();
            startActivity(new Intent(Splash.this, Splash2.class));
            finish();
        }
    }
                 }

0 个答案:

没有答案