Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Asynctask无法完成其任务_Android_Android Asynctask - Fatal编程技术网

Android Asynctask无法完成其任务

Android Asynctask无法完成其任务,android,android-asynctask,Android,Android Asynctask,希望你一切顺利。 我在应用程序中使用Asynctask从服务器获取数据和少量图像,并将它们存储在数据库和SD卡上。 当我在三星Pocket Plus上测试时,一切都很顺利,而当我在三星S3上测试时,Asynctask无法完成它的任务。 它只能从服务器下载一个映像,然后停止。 我不知道是什么问题,两部手机在测试时使用相同的互联网连接 class RequestRestaurants extends AsyncTask <Void, Void, Void> { @Overrid

希望你一切顺利。 我在应用程序中使用Asynctask从服务器获取数据和少量图像,并将它们存储在数据库和SD卡上。 当我在三星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();
        }
    }
                 }

您可以发布异步任务类代码吗?发布异步任务类代码您是否使用DownloadManager下载图像?我知道三星Galaxy S3上有一个bug,你必须自己编写下载管理器。这也可能是不同的问题,所以张贴您的代码;发布Logcat和你的AsynTask类。@David我是说每个看到这个问题的人:D:D