应用程序适用于WIFI但不适用于3G

时间:2016-12-21 19:32:36

标签: java android android-volley

有两项活动:MainDetail活动。

Main Activity基本上是一个GridView。 Detail Activity基本上显示了点击项目的详细信息。我将所选项目的ID(pid)从Main传递到Detail活动。

我面临的问题如下。最初,我有3G连接(手机连接)并点击了第一项,并在Detail Activity中查看了相应的项目详细信息,它完全正常,然后返回Main Activity,然后点击第二个项目,然后不幸的是它仍然显示我最初点击的DetailActivity中的第一个项目。

当应用处于有效状态且已打开时,我从3g切换到wifi。无论我点击什么,它仍然显示我最初点击的第一个项目。

但是,当我删除该应用并重新安装并获得wifi访问 时,该应用就可以正常使用。

在以下实施中,连接网址(PRODUCT_DETAIL_URL)为http,而非https。我正在使用Volley库进行网络连接。

DetailActivity.java

private void productDetailInit() {
      // it is http
        StringRequest postRequest = new StringRequest(Request.Method.POST, Config.PRODUCT_DETAIL_URL,
                new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                            try {
                                jsonObject = response;
                                loadJsonData();
                            } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
            ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("id", productID);
                return params;
            }
        };

        RetryPolicy policy = new DefaultRetryPolicy(1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        postRequest.setRetryPolicy(policy);
        CustomVolleyRequest.getInstance(this).getRequestQueue().add(postRequest);
    }

CustomVolleyRequest.java

public class CustomVolleyRequest {

private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;

private CustomVolleyRequest(Context context) {
    this.context = context;
    this.requestQueue = getRequestQueue();

    imageLoader = new ImageLoader(requestQueue,
            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);
                }
            });
}

private class BitmapCache implements ImageLoader.ImageCache {

    private LruCache<String, Bitmap> mCache;

    public BitmapCache() {
        mCache = new LruCache<>(20);
    }

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

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        // scaling bitmap for avoiding too much big images
        Bitmap scaled = ImageUtil.getInstance().scaleBitmap(bitmap);
        mCache.put(url, scaled);
    }
}

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

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        requestQueue = new RequestQueue(cache, network);
        requestQueue.start();
    }
    return requestQueue;
}

 public ImageLoader getImageLoader() {
    return imageLoader;
 }

}

Adapter.java

 class ProductMainAdapter extends ArrayAdapter<ImageRecord> {
    private ImageLoader mImageLoader;
    private String jsonObject;

    ProductMainAdapter(Context context) {
        super(context, R.layout.grid_item);
        mImageLoader = CustomVolleyRequest.getInstance(this.getContext()).getImageLoader();
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
            convertView.setBackgroundResource(R.drawable.round_gridview);
            holder.priceTagImage = (ImageView) convertView.findViewById(R.id.priceTag_IV);
            holder.textView = (TextView) convertView.findViewById(R.id.text);
            holder.imageView = (NetworkImageView) convertView.findViewById(R.id.picture);
            holder.priceTagRL = (RelativeLayout) convertView.findViewById(R.id.priceTag_RL);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageRecord imageRecord = getItem(position);
        holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openProductDetail(position);
            }
        });
        holder.textView.setText(imageRecord != null ? imageRecord.getTitle() : null);
        holder.priceTagRL.setRotation(0);

        return convertView;
    }

    private class ViewHolder{
        TextView textView;
        ImageView priceTagImage;
        NetworkImageView imageView;
        RelativeLayout priceTagRL;
    }

    private void openProductDetail(int position) {
        try {
            ImageRecord imr = getItem(position);
            String productID = imr != null ? imr.getId() : "0";
            Intent intent = new Intent(getContext(), ProductDetailActivity.class);
             intent.putExtra("pid", productID);
             getContext().startActivity(intent);    
        } catch (Exception e) {
            Log.e("openProductDetail", "exception", e);
        }
    }

0 个答案:

没有答案