将数据发送到Arraylist Android JSOUP

时间:2016-10-02 06:49:07

标签: java android arraylist

我正在从网站下载数据,并希望将其放入ArrayList中。我正在下载2个表单,图片和URL。

  ArrayList<String> artistNames = new ArrayList<String>();
    ArrayList<String> artistImageURLS = new ArrayList<String>();
    int chosen = 0;


    public class nameGetterClass extends AsyncTask<Void, Void, String>{

       Handler handle;
        Document doc;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... strings) {
            try {
                String url = "http://www.billboard.com/charts/artist-100";
                doc = Jsoup.connect(url).get();
                Elements names = doc.select("div.chart-row__title > h2.chart-row__song");
                for (Element p : names)
                    artistNames.add(p.text());
                  //  Log.i("names,", p.text());
            }
            catch(IOException ex){
            }
            return null;
        }
        @Override  `**<--- ERROR HERE: method does not override superclass**`
        protected void onPostExecute(Void result) {
        }
        }

我收到错误,如第二行最后一行所述。

这实际上甚至没有给arraylist添加任何内容,我现在该怎么办?

2 个答案:

答案 0 :(得分:0)

protected String doInBackground(Void... strings) {
            try {
                String url = "http://www.billboard.com/charts/artist-100";
                doc = Jsoup.connect(url).get();
                Elements names = doc.select("div.chart-row__title > h2.chart-row__song");
                for (Elements p : names)
                    artistNames.add(p.text());
                  //  Log.i("names,", p.text());
            }
            catch(IOException ex){
            }
            return null;
        }

请使用元素更新foreach内部的行。如果您还有任何问题,请尝试粘贴控制台输出,这将在模拟器中运行时获得,让人们了解真正的问题。

答案 1 :(得分:0)

尝试使用排球库。它简单而且更新 https://developer.android.com/training/volley/request.html

ArrayList<String> artistImageURLS = new ArrayList<String>();

String url = "http://my-json-feed";

JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {

       artistImageURLS.add(response.toString()); 

    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub

    }
});

// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);

这里是Singleton:

public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private MySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap>
                    cache = new LruCache<String, Bitmap>(20);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
}