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)
感謝它工作,,錯誤得到解決..現在我得到我在「Log.d(」響應:「,」>「+ jsonstr)中顯示的圖像URL;」但我無法在imageurls數組中獲得該圖像..我發現錯誤.. ?? – Akshay
@Aksh如果你有任何新的查詢,你可以問一個[新問題](http://stackoverflow.com/questions/ask) –