此代码仅用于解决不确定的进度条状态,仅适用于method1(),但不适用于method2()和method3()。问题已发布在其他线程中。 Indeterminate Progress-Bar Situation
// LoginActivity页面的进度条 公共类LoginActivity扩展了AppCompatActivity {
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) {
new ProgressBarTask().execute();
}
});
}
// ********************************************
// ********************************************
//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 {
/*
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));
}
/*
asynctask for the indeterminate progressBar with method1() and network
connection...
*/
class ProgressBarTask extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... voids) {
//checking network connectivity...
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();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressBar.setVisibility(View.GONE);
}
} }