问题描述
我是android的新手,我想在将数据发送到API时显示进度条,而当应用程序完成数据发送后,进度条将关闭。
我不确定如何使用已有的代码来实现进度。 请一些帮助?
这是我用来将数据发送到我的API的代码:
sendDataBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RequestModel data = helper.sendData(Integer.parseInt(imt));
request = new ApiRequest(MainActivity.this, APIMethod.POST, data);
try {
String response = request.execute("myURL").get();
ResponseModel responseModel = new ResponseModel(response);
if (responseModel.isSuccess()) {
Toast.makeText(getApplication(), "OK", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplication(), "ERROR", Toast.LENGTH_SHORT).show();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
});
1楼
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
在java中:private ProgressBar spinner; spinner =(ProgressBar)findViewById(R.id.progressBar1);
运行进度:spinner.setVisibility(View.VISIBLE);
要停止进度:spinner.setVisibility(View.GONE);
2楼
您可以使用AsyncTask来做到这一点。 在OnClickListener中处理代码时,正在使用主线程,因此UI将在执行期间被阻塞,因此使用AsyncTask时,您的代码将在另一个后台线程上执行。
您可以创建一个侦听器:
public interface AsyncTaskListener {
void onStart();
void onSuccess();
void onError(Exception ex);
}
您的AsyncTask类:
public class CustomAsyncTask extends AsyncTask<Integer, Void, CustomAsyncTask.ProcessingResult> {
private AsyncTaskListener mListener;
public CustomAsyncTask (AsyncTaskListener aListener) {
mListener = aListener;
}
@Override
protected void onPreExecute(){
mListener.onStart();
}
@Override
protected CustomAsyncTask.ProcessingResult doInBackground(Integer... params) {
RequestModel data = helper.sendData(params[0]);
request = new ApiRequest(MainActivity.this, APIMethod.POST, data);
try {
String response = request.execute("myURL").get();
ResponseModel responseModel = new ResponseModel(response);
if (responseModel.isSuccess()) {
return new ProcessingResult();
} else {
return new ProcessingResult(new Exception("Error message"));
}
} catch (InterruptedException|ExecutionException e) {
return new ProcessingResult(e);
}
}
@Override
protected void onPostExecute(ProcessingResult aResult) {
Exception error = aResult.getError();
if(error == null){
mListener.onError(error);
} else {
mListener.onSuccess();
}
}
private class ProcessingResult {
private Exception mError;
ProcessingResult () {
}
ProcessingResult (Exception aError) {
mError = aError;
}
Exception getError() {
return mError;
}
}
}
添加到布局XML:
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
您的活动课程:
public class MainActivity extends AppCompatActivity {
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mProgressBar = findViewById(R.id.progressBar);
Button yourButton = findViewById(R.id.your_button_id);
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CustomAsyncTask asyncTask = new CustomAsyncTask(new CustomListener());
asyncTask.execute(Integer.parseInt(your_integer));
}
});
}
private class CustomListener implements AsyncTaskListener {
public void onStart() {
mProgressBar.setVisibility(View.VISIBLE);
}
public void onSuccess() {
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.GONE);
}
public void onError(Exception ex) {
Toast.makeText(MainActivity.this, ex.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.GONE);
}
}
}