我们如何加载以前加载的图像在android volley

时间:2016-04-29 12:57:16

标签: android android-volley

*********我正在创建具有列表视图的应用程序,它显示网络图像视图和文本视图*********

当互联网开启但当我进入离线模式时,它不会在高速缓冲存储器中显示我之前加载的图像。我的主要类的代码在我尝试离线工作时直接转到json数组这是错误的。 ..

 public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private ListView listView;
    private CustomListAdapter listAdapter;
    private List<item> items;
    private ProgressDialog mdialog;
    private String URL_FEED = "json URL"


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        items = new ArrayList<item>();
        listAdapter = new CustomListAdapter(this, items);
        listView.setAdapter(listAdapter);
        Cache cache = AppController.getInstance().getRequestQueue().getCache();
        Entry entry = cache.get(URL_FEED);
        if (entry != null)
        {
            // fetch the data from cache
            try {
                String data = new String(entry.data, "UTF-8");

                Log.d("Internet NO", "Response: " + data);
                try {
                    //parseJsonFeed(new JSONObject(data));
                    JSONArray jsonArray=new JSONArray(data);
                    setData(jsonArray,true);
                    Toast.makeText(getApplicationContext(), "Loading from cache.", Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }else {

            callJsonArrayRequest();` }

    }

    private void callJsonArrayRequest()
    {
        // TODO Auto-generated method stub
//        showDialog();
        JsonArrayRequest jsonarrayReq = new JsonArrayRequest(URL_FEED,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        setData(response,false);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                //dismissDialog();
            }
        });
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonarrayReq);
    }
    private void setData(JSONArray response, Boolean isCache) {
        Log.d(TAG, response.toString());
        try {
            for (int i = 0; i < response.length(); i++) {
                JSONObject person = (JSONObject) response.get(i);
                item model=new item();
                model.setSname(person.getString("Name"));
                model.setPimage(person.getString("image"));
                items.add(model);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
        }
        listAdapter.notifyDataSetChanged();
        if(!isCache){
            Toast.makeText(getApplicationContext(), "Cache not available..Loading from service", Toast.LENGTH_SHORT).show();
            //dismissDialog();
        }
    }
`

0 个答案:

没有答案