I added a progressbar
as indeterminate state and put in a class extends asynctask
.
In doInBackground
method, i added one method (say m_one
) with volley-library
.
In m_one()
, i have called method m_two();
.
In m_two()
, i have called m_three();
.
Now, the problem is, progressbar
only works till m_one()
executes and then progressbar
visibility goes invisible and remaining methods executes in background thread.
How do i keep rotating progressbar
until all the methods done receiving data using volley request?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Try this code.
private static final String TAG = "LoginActivity.this";
Button button_login;
Context context;
ProgressBar progressBar;
//some variables for accepting the volley response data
DBUtils dbUtils;
Calendar calendar_2;
public SimpleDateFormat dateFormat;
public SimpleDateFormat timeformat;
Date date;
TreeSet<Integer> idSet;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
button_login = findViewById(R.id.btn_login);
context = getApplicationContext();
progressBar = findViewById(R.id.progress_id_1);
calendar_2 = Calendar.getInstance();
dbUtils = new DBUtils(this);
//clear the db tables on start of app
dbUtils.ClearTables();
date = Calendar.getInstance().getTime();
Log.e(TAG, "\n current time: " + date);
dateFormat = new SimpleDateFormat("dd-MM-yyyy");
idSet = new TreeSet<>();
//Login button click event ********************
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkNetwork();
}
});
}
private void checkNetwork() {
progressBar.setVisibility(View.VISIBLE);
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
try {
if (netInfo != null) {
NetworkInfo.State state = conMgr.getActiveNetworkInfo().getState();
if (state == NetworkInfo.State.CONNECTED) {
Log.e(TAG, "nw info: " + netInfo.getExtraInfo());
Log.e(TAG, "Network Connected ");
method1();
Thread.sleep(3000);
} else {
Log.e(TAG, "Network Disconnected");
}
} else if (netInfo == null) {
Log.e(TAG, "Network Not Found !");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// ********************************************
// ********************************************
//to get the response of requested url
public void method1() {
try {
//url without api for login session
URL loginURL = new URL(your url 1);
HttpURLConnection urlconnect = (HttpURLConnection) loginURL.openConnection();
;
//http connection for webpage permission
//type of request get / post...
urlconnect.setRequestMethod("POST");
//allow the post request for user
urlconnect.setDoOutput(true);
/*
Singleton_Volley is the class for volley initialize
you can skip Singleton_Volley class..its correct
*/
// Get a RequestQueue in required page
RequestQueue queue = Singleton_Volley.getInstance(this.getApplicationContext()).
getRequestQueue();
// Request a string response from the provided URL.(my case, url)
StringRequest stringRequest = new StringRequest(Request.Method.POST, "" + loginURL.toString(),
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//accept the data from request and set in our views like list,text, etc...
try {
/* your response data stored in variables first and then stored in sqlite you have to consider this strictly, make sure you also have array in response to wait for a while */
method2();
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//popup an error message...(Toast)
}
})
} ;
// Add the request to the RequestQueue.
Singleton_Volley.getInstance(this).addToRequestQueue(queue.add(stringRequest));
} catch(
IOException e)
{
e.printStackTrace();
} catch(
JSONException e)
{
e.printStackTrace();
}
}
public void method2() {
try {
url_2 = new URL(your url 2);
HttpURLConnection urlconnect = (HttpURLConnection)
url_2.openConnection();
;
//http connection for webpage permission
//type of request get / post...
urlconnect.setRequestMethod("POST");
//allow the post request for user
urlconnect.setDoOutput(true);
RequestQueue contactQueue = Singleton_Volley.getInstance(this).
getRequestQueue();
// Request a string response from the provided URL.(in my case, url_2)
StringRequest stringRequest = new StringRequest(Request.Method.GET, "" + url_2.toString(),
new Response.Listener<String>() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(String response) {
//accept the data from request and set in our views like list,text, etc...
try {
/*
your response data stored in variables first and then stored in sqlite
you have to consider this strictly
make sure you also have array in response to wait for a while
*/
method3(); //method3() called here
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//popup an error messege...(Toast)
Toast.makeText(LoginActivity.this, "" + error, Toast.LENGTH_SHORT).show();
}
})
} ;
// Add the request to the RequestQueue.
Singleton_Volley.getInstance(this).addToRequestQueue(contactQueue.add(stringRequest));
} catch(Exception e){
e.printStackTrace();
}
}
// ********************************************
// ********************************************
private void method3(){
try{
URL url_3=new URL(your url 3);
HttpURLConnection urlconnect=(HttpURLConnection)url_3.openConnection();
;
//http connection for webpage permission
//type of request get / post...
urlconnect.setRequestMethod("POST");
//allow the post request for user
urlconnect.setDoOutput(true);
RequestQueue stageQueue=Singleton_Volley.getInstance(this).
getRequestQueue();
// Request a string response from the provided URL.(in my case, url_3)
StringRequest stringRequest=new StringRequest(Request.Method.GET,""+url_3.toString(),
new Response.Listener<String>(){
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(String response){
//accept the data from request and set in our views like list,text, etc...
try{
progressBar.setVisibility(View.GONE);
/*
make sure you also have array in response to wait for a while
*/
}catch(Exception ex){
ex.printStackTrace();
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
//popup an error messege...(Toast)
}
})
};
// Add the request to the RequestQueue.
Singleton_Volley.getInstance(this).addToRequestQueue(stageQueue.add(stringRequest));
}catch(Exception ec){
ec.printStackTrace();
}
startActivity(new Intent(LoginActivity.this,Page2.class));
}
}
}}
Please manage the closing } and it should work.