gpt4 book ai didi

java - Volley 适用于 WIFI 但不适用于 3G

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:21 24 4
gpt4 key购买 nike

如果问题太长,我先道歉。我们开始吧:

有两个 Activity :MainDetail Activity。

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

我面临如下问题。最初,我有 3G 连接(蜂窝连接)并单击第一个项目并在 Detail Activity 中查看相应的项目详细信息,它工作得很好,然后返回Main Activity,然后单击第二个项目,不幸的是它仍然显示我最初单击的 DetailActivity 中的第一个项目。

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

但是,当我删除该应用程序并重新安装它并获得 wifi 访问权 时,该应用程序运行良好。

在以下实现中,连接 URL (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);
}
}

我想知道我在提供的实现中遗漏了什么/做错了什么。已经花了将近 2-3 个月的时间,我无法处理这个问题。有没有人遇到过类似的情况?非常感谢任何建议或评论。

最佳答案

您可以使用 finish() 终止 Activity ;当另一个加载时。

关于java - Volley 适用于 WIFI 但不适用于 3G,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com