2014-09-19 81 views
-1

在我的應用程序中,我從服務器上獲取圖像和數據。當互聯網連接有圖像加載正確。當互聯網連接速度減慢時,應用程序會繼續運行,並在某段時間後突然停止。應用程序停止一段時間後下載圖像緩慢連接

這是AsyncTask從服務器獲取的圖像和數據:

public void getPendingList() { 
    new AsyncTask<Void, Void, String>() { 
     protected void onPreExecute() { 
      progressDialog = new ProgressDialog(getActivity()); 
      progressDialog.setTitle("Loading...."); 
      progressDialog.setMessage("Please Wait..."); 
      progressDialog.setCancelable(true); 
      progressDialog.show(); 
     } 

     protected String doInBackground(Void... params) { 
      prepareList(); 
      getRegistrationDetails(); 
      getStateList(); 
      for (int i = 0; i < districts.size(); i++) { 
       JSONObject jsonObject; 
       try { 
        jsonObject = new JSONObject(districts.get(i)); 

        String val1 = jsonObject.getString("OptionalParameter"); 
        int val2 = jsonObject.getInt("ItemID"); 
        String val3 = jsonObject.getString("Item_Description"); 
        double val4 = jsonObject.getDouble("Item_Price"); 
        String val5 = jsonObject.getString("Item_Code"); 
        String val6 = jsonObject.getString("Item_Name"); 
        String val7 = jsonObject.getString("ImageFinal"); 
        String ItemCode = jsonObject.getString("Item_Code"); 

        val7.replace("/", ""); 

        listmodel.add(new Item_Master(val2, val3, val4, val6, val7, ItemCode)); 
        listCountry.add("\u20B9" + val4); 

       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      return "response"; 
     } 

     protected void onPostExecute(String result) { 
      if (result != null) { 
       if (!result.equals("NO_NETWORK")) { 
        mAdapter = new GridviewAdapter_Item_Master(FaramentView.getContext(), listmodel); 
        gridview.setAdapter(mAdapter); 
       } 
       dismissDialog(); 
      } 
     }.execute(null, null); 
    } 
} 

得到這個數據後,我在設定這個數據GridView適配器和圖像設置。我使用畢加索圖書館下載圖像並設置以下代碼:

Picasso.with(context).load(list.get(position).getImagePath()).centerCrop() 
    .resize(150, 150).error(R.drawable.ic_launcher).into(view.imgViewFlag); 

如何在緩慢的互聯網連接中加載圖像?

回答

0

你得到一個ANR可能,這意味着你趕上UI超時,因爲你想在更新UI的應用組件和AsyncTask(但牢記doInBackground()方法來管理後臺網絡處理) ;更新UI應用程序組件,您可以使用Handlers或更新它runOnUiThread()

+0

謝謝我將嘗試 – user3651987 2014-09-19 12:13:46

+0

讓我知道如果適合和案例投票了,請 – 2014-09-19 12:53:01

相關問題