gpt4 book ai didi

java - 如何停止从网络服务器接收数据?

转载 作者:行者123 更新时间:2023-11-29 02:28:05 26 4
gpt4 key购买 nike

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

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

这个问题是退出activity,因为当停留在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;

}
}

最佳答案

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

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

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

关于java - 如何停止从网络服务器接收数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076942/

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