0

我正在开发应用程序以json webservicesgridview. 当我直接给出网址时,代码是成功的。

下面是我的代码:

 package com.example.sample;

 import java.io.IOException;
 import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONObject;
import com.example.sample.R;


import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

    GridView list;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(GridView)findViewById(R.id.list);
        adapter=new LazyAdapter(this, mStrings);
        list.setAdapter(adapter);
        adapter.imageLoader.clearCache();
        adapter.notifyDataSetChanged();


    }


    private String[] mStrings={
            "http://www.1.png",
            "http://www.2.png",
            "http://www.3.png",
            "http://www.4.png",
            "http://www.5.png"



    };
}

在这里,它在 gridview 中正确地向我显示了这五个图像。现在我的问题是如何将 url 列表从我的 AsynchronousTask 直接发送到 gridview 中

下面是我的AsynchronousTask code

private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>> {

        protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
           InputStream in = entity.getContent();
             StringBuffer out = new StringBuffer();
             int n = 1;
             while (n>0) {
                 byte[] b = new byte[4096];
                 n =  in.read(b);
                 if (n>0) out.append(new String(b, 0, n));
             }
             return out.toString();
        }

        @Override
        protected ArrayList<String> doInBackground(Void... params) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();
         HttpGet httpGet = new HttpGet("myurl");
         String text = null;
         String result = null;
         ArrayList<String> imageurl=new ArrayList<String>();
         String name=null;
         String id;

         try {
               HttpResponse response = httpClient.execute(httpGet, localContext);
               HttpEntity entity = response.getEntity();
               text = getASCIIContentFromEntity(entity);





                   JSONArray ja = new JSONArray(text) ;
                   // ITERATE THROUGH AND RETRIEVE CLUB FIELDS
                                int n = ja.length();
                                for (int i = 0; i < n; i++) {
                                    // GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY
                                 JSONObject jo = ja.getJSONObject(i);

                                 imageurl.add(jo.getString("ImageUrl"));
                                 name= jo.getString("ImageName");

                                  id= jo.getString("ImageId");



                                }
         } catch (Exception e) {
                      e.getLocalizedMessage();
                   }


         return imageurl;


        }       


    protected void onPostExecute(ArrayList<String> results){
            if(results!=null){
    for(int j=0;j<results.size();j++){

           String[] mStrings={results.get(j)};
    }
}


}

}

我的 LazyAdapter 代码是:

    public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);


        ImageView image=(ImageView)vi.findViewById(R.id.image);

        imageLoader.DisplayImage(data[position], image);
        return vi;
    }
}

怎么寄imageurl(Arraylist)进去mStrings(string array)

4

1 回答 1

0

将您的 AsyncTask 类更改为:

   private class LongRunningGetIO extends AsyncTask <String, Void, ArrayList<String>> {

.........

     @Override
        protected ArrayList<String> doInBackground(String... params) {

            // run a loop here for the length of params which will download images one after other.
..........
      }
}
于 2013-02-15T14:04:42.793 回答