2014-03-04 43 views
0

您好我想GridView演示构成本link将图像载入ImageAdapter

,它高超的工作,但现在我想对服务器database.For说我实现了处理程序类,将通过我的URL的图片并从服务器获取响应。作为响应,JSON将包含图像的URL。我还实现了一种方法,它将下载图像形式生成的URL并将它们设置为Bitmap阵列。 服务处理程序类正常工作,但我不知道我在ImageAdapter类中所做的更改是否正确..请请告诉我,我得到错误.. 以下为ImageAdapter类代码..

 public class ImageAdapter extends BaseAdapter { 
     private Context mContext; 
     Bitmap bm[]=new Bitmap[8]; 
     ImageAdapter img; 
     public String[] imageurls=null; 

     // Keep all Images in array 
     public Integer[] mThumbIds ={ 
       R.drawable.pic_1, R.drawable.pic_2, 
       R.drawable.pic_3, R.drawable.pic_4, 
     }; 


     // Constructor 
     public ImageAdapter(Context c){ 
      mContext = c; 
      img =new ImageAdapter(mContext); 
      new GetImageUrls(mContext).execute(); 

     } 

     @Override 
     public int getCount() { 
      return mThumbIds.length; 
     } 

     @Override 
     public Object getItem(int position) { 
      return mThumbIds[position]; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) {   
      ImageView imageView = new ImageView(mContext); 
      if(bm[position]==null) 
        imageView.setImageResource(mThumbIds[position]); 
      else 
       imageView.setImageBitmap(bm[position]); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setLayoutParams(new GridView.LayoutParams(70, 70)); 
      return imageView; 
     } 


     public class GetImageUrls extends AsyncTask<Void, Void, Void> 
     { 
      Context context; 
      private ProgressDialog pDialog; 
      // URL to get JSON 
      private static final String url= "http://192.xxx.x.xxx/Demo_Folder/test.json"; 
      private static final String RESULT = "mainCategory"; 
      private static final String URLS = "mcatimage"; 
      // JSONArray 
      JSONArray loginjsonarray=null; 
      //result from url 
      //public String[] imageurls=null; 

      public GetImageUrls(Context c) { 
       this.context=c; 
      } 

      protected void onPreExecute() { 
       // Showing progress dialog 
       pDialog = new ProgressDialog(context); 
       pDialog.setMessage("Loading..."); 
       pDialog.setCancelable(false); 
       //pDialog.show(); 
      } 
      protected Void doInBackground(Void... arg) { 
       // Creating service handler class instance 
       ServiceHandler sh = new ServiceHandler(); 
       // Making a request to url and getting response 
       String jsonstr = sh.makeServiceCall(url, ServiceHandler.GET, null); 
       Log.d("Response: ", ">"+jsonstr); 
       if(jsonstr!=null){ 
        try { 
          JSONObject jsonObj =new JSONObject(jsonstr); 
          loginjsonarray=jsonObj.getJSONArray(RESULT); 
          for(int i=0;i<loginjsonarray.length();i++){ 
           JSONObject l=loginjsonarray.getJSONObject(i); 
           imageurls[i]=l.getString(URLS); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
       }else{ 
        Toast.makeText(context,"Check your Internet Connection",Toast.LENGTH_SHORT).show(); 
       } 
       return null; 
      } 

      protected void onPostExecute(Integer result) { 
       // Dismiss the progress dialog 
       if(pDialog.isShowing()){ 
        pDialog.dismiss(); 
        new ImageDownloader().execute(); 
       } 

      } 
     } 

     private class ImageDownloader extends AsyncTask<Integer, Integer, String> 
     { 
      Context context; 
      private ProgressDialog pDialog; 
      @Override 
      public void onPreExecute() 
      { 
       // Showing progress dialog 
       pDialog = new ProgressDialog(context); 
       pDialog.setMessage("Loading..."); 
       pDialog.setCancelable(false); 
       //pDialog.show(); 
      } 

      protected void onPostExecute(String result) 
      { 
       pDialog.hide(); 
      } 
       protected void onProgressUpdate(Integer... progress) { 
        img.notifyDataSetChanged(); 
       } 
      protected String doInBackground(Integer... a) 
      { 
       for(int i=0;i<4;i++) 
       { 
        bm[i]=downloadBitmap(imageurls[i]); 
        this.publishProgress(i); 
       } 
       return ""; 
      } 
     } 

     Bitmap downloadBitmap(String url) { 

      final HttpClient client = new DefaultHttpClient(); 
      final HttpGet getRequest = new HttpGet(url); 

      try { 
       HttpResponse response = client.execute(getRequest); 
       final int statusCode = response.getStatusLine().getStatusCode(); 
       if (statusCode != HttpStatus.SC_OK) { 
        Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
        return null; 
       } 

       final HttpEntity entity = response.getEntity(); 
       if (entity != null) { 
        InputStream inputStream = null; 
        try { 
         inputStream = entity.getContent(); 
         Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
         return bitmap; 
        } finally { 
         if (inputStream != null) { 
          inputStream.close(); 
         } 
         entity.consumeContent(); 
        } 
       } 
      } catch (Exception e) { 
       // Could provide a more explicit error message for IOException or IllegalStateException 
       getRequest.abort(); 
       Log.e("ImageDownloader", "Error while retrieving bitmap from " + url); 
      } finally { 
       if (client != null) { 
        //client. 
       } 
      } 
      return null; 
     } 

    } 

当我运行它得到制止immediately..and代码给像

FATAL EXCEPTION: main 
Process: com.example.androidhive, PID: 1383 
java.lang.StackOverflowError 
at java.util.AbstractList.<init>(AbstractList.java:376) 
at java.util.ArrayList.<init>(ArrayList.java:81) 
at android.database.Observable.<init>(Observable.java:34) 
at android.database.DataSetObservable.<init>(DataSetObservable.java:24) 
at android.widget.BaseAdapter.<init>(BaseAdapter.java:31) 
at com.example.androidhive.ImageAdapter.<init>(ImageAdapter.java:40) 

回答

2

删除错误这条线

img =new ImageAdapter(mContext); 

来自构造函数ImageAdapter。它试图创建ImageAdapter情况下导致StackOverflowError无限数量...

,为什么你需要在ImageAdapterImageAdapter另一个实例?

+0

感谢它工作,,错误得到解决..现在我得到我在“Log.d(”响应:“,”>“+ jsonstr)中显示的图像URL;”但我无法在imageurls数组中获得该图像..我发现错误.. ?? – Akshay

+0

@Aksh如果你有任何新的查询,你可以问一个[新问题](http://stackoverflow.com/questions/ask) –