2

我有一个 Activity 接收和显示来自 web 服务器的信息并将其保存到数据库中,如果它已经收到过一次信息,它将从数据库中读取。

程序运行没有问题,只有当用户进入activity仍然没有收到服务器的信息时,退出activity,在另一个Activity中应用程序崩溃。

这个问题是退出活动,因为当我们完全停留在活动中并加载数据时,我们没有任何问题。从数据库读取数据时,我们也没有任何问题。

当用户退出活动或以另一种方式解决此问题时,如何停止接收数据。

这是我的活动代码:

public class ShowProduct extends AppCompatActivity {
private RequestQueue mQueue;
private String id;
private Products products;
private DatabaseHelper databaseHelper;
private ProgressBar progressBar;
private Typeface IranSans,IranSansBold;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_product);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

    databaseHelper = new DatabaseHelper(this);
    products  = new Products();
    progressBar = (ProgressBar)findViewById(R.id.progressBar);
    IranSans = Typeface.createFromAsset(getAssets(),"fonts/IRANSansWeb.ttf");
    IranSansBold = Typeface.createFromAsset(getAssets(),"fonts/IRANSansWeb_Bold.ttf");


    Bundle extras = getIntent().getExtras();
    id= extras.getString("id");
    try {
        if (databaseHelper.dbContentChecker(id)) {
            if (connectionCheck() == false){
                Intent intent = new Intent(this, NoInternetConnection.class);
                startActivity(intent);
            }
            String url = "https://website.com/webserver/?id=" + id;
            mQueue = Volley.newRequestQueue(this);
            jsonParse(url);
        } else {
                products = databaseHelper.getOneProduct(id);
                showFromDB(products);
        }
    }catch (Exception ex){
    }


    //footer set
    Footer footer = new Footer();
    footer.footerSet(ShowProduct.this, this);
}

private void showFromDB(Products products) {
    TextView tx = (TextView)findViewById(R.id.title);
    tx.setTypeface(IranSansBold);
    tx.setText(products.getName());
    ImageView im = (ImageView)findViewById(R.id.image);
    Glide.with(ShowProduct.this)
            .asBitmap()
            .load(products.getImage())
            .into(im);
    TextView tc = (TextView)findViewById(R.id.content);
    tc.setTypeface(IranSans);
    tc.setText(Html.fromHtml(products.getContent()));
    progressBar.setVisibility(View.GONE);

}


private void jsonParse(String url) {
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONArray jsonArray = response.getJSONArray("send");

                        JSONObject send = jsonArray.getJSONObject(0);

                        String title = send.getString("title");
                        String image = send.getString("image");
                        String content = send.getString("content");
                        TextView tx = (TextView)findViewById(R.id.title);
                        tx.setTypeface(IranSansBold);
                        tx.setText(title);
                        ImageView im = (ImageView)findViewById(R.id.image);
                        Glide.with(ShowProduct.this)
                                .asBitmap()
                                .load(image)
                                .into(im);
                        TextView tc = (TextView)findViewById(R.id.content);
                        tc.setTypeface(IranSans);
                        tc.setText(Html.fromHtml(content));



                        //Save in Database
                        try {
                            products.setProductId(id);
                            products.setName(title);
                            products.setImage(image);
                            products.setContent(content);
                            databaseHelper.editProduct(products);
                        }catch (Exception ex){
                            Toast.makeText(ShowProduct.this, "Error DB", Toast.LENGTH_SHORT).show();
                        }

                        progressBar.setVisibility(View.GONE);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    mQueue.add(request);
    //Toast.makeText(getApplicationContext(), ""+Temp , Toast.LENGTH_SHORT).show();

}



public boolean connectionCheck() {
    ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
        //we are connected to a network
        return true;
    }
    else
        return false;

}
}
4

1 回答 1

0

首先,您应该考虑您的架构。在 Activity 中处理获取数据并不是一个好习惯。查看 MVVM 或 MVP 并尝试将您的数据获取逻辑放在另一层。

对于您的具体问题,您应该尝试在 Activity 的 onStop() 方法中取消请求,请参阅:

https://developer.android.com/training/volley/simple#cancel

于 2018-06-28T07:39:10.943 回答