This is my code
I want I show the progressdialog until receive the data from server,
and when I got result from the server(if items.size>0), progressdialog will be dismiss;
But I run this code, I receive the data from the server but in runonuiThread, still running progressdialog.
List<ServerData> items = new ArrayList();
progressdialog.show();
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
HttpRequest...
Gson gson = new Gson();
Type type = new TypeToken<List<ServerData>>(){}.getType();
items = gson.fromJson(String,type);
Log.d("ThreadInfo","" + items.size);
runOnUiThread(new Runnable() {
#Override
public void run() {
if(items.size > 0){
progressdialog.dismiss();
}
}
});
}
});
When I Log the ThreadInfo, It sends items.size value is 1
but still progressdialog is running and not disappear.
Question,
How I can dismiss the progressdialog when items.size is not 0?
If you have any idea, help me please.
You have to change your code like this
if (items.size > 0) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progressdialog.dismiss();
}
});
}
Related
I am getting an error on the last line below after replacing my asynctask with a runnable to upload data to server: Any pointers will be appreciated.
Cannot return a value from a method with void result type
on this line
return *imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
ExecutorService service = Executors.newSingleThreadExecutor ( );
service.execute ( new Runnable ( ) {
#Override
public void run() {
//preexecute
runOnUiThread ( new Runnable ( ) {
#Override
public void run() {
progressDialog = ProgressDialog.show( UploadActivity.this,"Your Data is Uploading To Our Servers",
"Please Wait And Ensure Data is On With Bundles. 3G or WiFi data is preferred",false,false);
}
} );
//doinbackground
ImageProcessClass imageProcessClass = new ImageProcessClass ();
HashMap<String,String> HashMapParams = new HashMap<>();
HashMapParams.put(USER_NAME, userName);
HashMapParams.put(USER_EMAIL, userEmail);
HashMapParams.put(USER_PHONE, userPhone);
HashMapParams.put(ImagePath1, imageView1);
return imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
The problem is that run() has no return value where your previous solution with AsyncTask allows a return value.
I expect this return something.
imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
but this has void as a return value
#Override
public void run() {
...
}
I think you need a new solution for returning the result, maybe working with a Listener and trigger the callback?
interface ResultListener{
public void onResult(result: ResultType)
}
#Override
public void run() {
...
listener.onResult(imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);)
}
// Sorry I'm not very familiar with Java anymore, hope you get the idea :)
I have same stock item , I want to send local database to ApiService, But when I send also I want to update ProgressBar message. I tried the code below but it just shows when all proccessing is finishing.
ProgressDialog progress= new ProgressDialog(this);
progress.setTitle(getResources().getString(R.string.progress_exporting));
progress.setMessage("0/0");
when click button I call below method
public void Export() {
runOnUiThread(new Runnable() {
#Override
public void run() {
findViewById(R.id.btnExportOnlineWithStocktaking).setEnabled(false);
progress.show();
}
});
UpdateUI(send, total);
try {
switch (_stocktakingType) {
case Division: {
switch (_onlineExportType) {
case Item: {
isExport = ExportDivisionStocktakingItems(stocktakingId);
}
break;
}
} catch (Exception ex) {
}
}
// ExportDivisionStocktaking method
public boolean ExportCustomStocktakingItems(int stocktakingId) {
result = Boolean.parseBoolean(SendCustomStocktakingItems(stocktakingId,countResults).responseString);
}
My call back method
public ResponseModel SendCustomStocktakingItems(int selectedDivision, List<ExtensionServiceStocktakingItem> countResults) throws ExecutionException, InterruptedException {
return new SendCustomStocktakingItemsService(flag -> true).execute(String.valueOf(selectedDivision), countResults.toString()).get();
}
//AsyncTask method
public class SendDivisionStocktakingItemsService extends AsyncTask<String, Void, ResponseModel> {
public AsyncResponseSendDivisionStocktakingItems delegate = null;
public SendDivisionStocktakingItemsService(AsyncResponseSendDivisionStocktakingItems delegate) {
this.delegate = delegate;
}
#Override
protected ResponseModel doInBackground(String... parameters) {
RequestHandler requestHandler = new RequestHandler();
JSONObject params = new JSONObject();
try {
params.put("stocktakingItems", parameters[1]);
} catch (JSONException e) {
e.printStackTrace();
}
ResponseModel responseModel = requestHandler.getRequestPostString(UHFApplication.getInstance().apiUrl
+ "/api/MobileService/SendDivisionStocktakingItemsPost?stocktakingID="
+ parameters[0],
parameters[1]);
return responseModel;
}
#Override
protected void onPreExecute() {
UpdateUI(send,total);
super.onPreExecute();
}
#Override
protected void onPostExecute(ResponseModel responseModel) {
super.onPostExecute(responseModel);
if (HttpURLConnection.HTTP_OK == responseModel.httpStatus) {
delegate.processFinish(true);
} else {
delegate.processFinish(false);
}
}
}
//UICalled method
public void UpdateUI(int send, int total) {
runOnUiThread(() -> {
progress.setMessage(send + "/" + total);
Log.d("Send Data : ", send + "/" + total);
if (send == total) {
progress.dismiss();
Toast.makeText(getApplicationContext(), "Succsess", Toast.LENGTH_SHORT).show();
}
});
}
//Update
//Ok I have a simle example how can I use. Below code when I click button I wan to open progress firstly and after that for loop is working and update progres message. I try it but not working.
Firstly For loop is working and after that progres opened.
public void ExportTry(){
UpdateUI(send,total);
runOnUiThread(new Runnable() {
#Override
public void run() {
btnExport.setEnabled(false);
progress.show();
}
});
for(int i=0;i<1000000;i++){
UpdateUI(i,1000000);
}
}
You are missing the part of AsyncTask that will allow you to show progress messages while doInBackground is running. Take a look at onProgressUpdate and publishProgress on the same page.
publishProgress
void publishProgress (Progress... values)
This method can be invoked from doInBackground(Params...) to publish updates on the UI thread while the background computation is still running. Each call to this method will trigger the execution of onProgressUpdate(Progress...) on the UI thread. onProgressUpdate(Progress...) will not be called if the task has been canceled.
I load my articles from my wordpress webpage. And want to display them in my app.
In order to be allowed to wait for a internet response I had to create a Thread.
Using this method:
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
#Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
Then in the createArticles function I try to use my function addViewToParent(...) and I get an error that says:
Only the original thread that created a view hierarchy can touch its
views.
How can i work around this problem?
EDIT: I use these networking functions if it matters...
//Connect to the url
URL pageURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) pageURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
If you want to modify your Views, e.g. show your article, you have to run the code on the UI/main thread. To achieve this, you could call run runOnUiThread() in your Activity:
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
#Override
public void run() {
try {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
You need to use function runOnUiThread. This is function for user interface thread.
private void loadArticles(final String url) {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
});
Just use a Handler instead, so you'll have access to the UI Thread:
new Handler().post(new Runnable(){
#Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
I make a game about 8Puzzle on android with AI using A* algorithm.
Everything works fine but there is a problem, there are some function that is executed in parallel.
What I want is the function is executed after another function is finished.
Here is code:
if(AItype.equals("A*"))
{
DisableButton();
DisableClickImageView();
AStarSolver as = new AStarSolver();
as.solvePuzzle(arr, GOAL); //Solve the puzzle
displayResult(as.solutionPath); //display animation
as = null;
Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_LONG).show();
copySTARTtoArray();
setImageResource();
EnableButton();
}
I want text "Finished" is displayed after function displayResult() is finished but the text "Finished"
show at the same time with function displayResult().
How to solve this??
Edit:
Here is the code for displayResult();
public void displayResult(final Stack<Node> solutionPath)
{
Handler handler = new Handler();
for (int i = 0; i < solutionPath.size(); i++)
{
handler.postDelayed(new Runnable() {
public void run() {
Node solNode = solutionPath.pop();
//solNode.NodeState.
tile00.setImageResource(solNode.getImageResourceBasedNodeState(0));
tile01.setImageResource(solNode.getImageResourceBasedNodeState(1));
tile02.setImageResource(solNode.getImageResourceBasedNodeState(2));
tile10.setImageResource(solNode.getImageResourceBasedNodeState(3));
tile11.setImageResource(solNode.getImageResourceBasedNodeState(4));
tile12.setImageResource(solNode.getImageResourceBasedNodeState(5));
tile20.setImageResource(solNode.getImageResourceBasedNodeState(6));
tile21.setImageResource(solNode.getImageResourceBasedNodeState(7));
tile22.setImageResource(solNode.getImageResourceBasedNodeState(8));
}
}, 800 * (i + 1));
}
}
It display the result of animation (Ex: tile0 to tile1)
Try this.
public void displayResult(final Stack<Node> solutionPath)
{
Handler handler = new Handler();
for (int i = 0; i < solutionPath.size(); i++)
{
handler.postDelayed(new Runnable() {
public void run() {
Node solNode = solutionPath.pop();
//solNode.NodeState.
tile00.setImageResource(solNode.getImageResourceBasedNodeState(0));
.
.
.
tile22.setImageResource(solNode.getImageResourceBasedNodeState(8));
}
}, 800 * (i + 1));
}
// Put following code
handler.postDelayed(
new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_LONG).show();
}
},800*(solutionPath.size()+1)
);
}
Your code should be look like this.
solvePuzzle(...){
// solved #1
displayResult(...){
}
// solved #2
displayResult(...){
}
}
or use thread.
as.solvePuzzle(arr, GOAL); //Solve the puzzle
displayResult(as.solutionPath); //display animation
means solvePuzzle is all done, then call displayResult.
function(){
//code of this function
function1();
}
function1(){
//code of this function
function2();
}
function2(){
}
I am trying to query a web service using loopJ and during this operation I want to show the users a progress dialog. When the operation is complete, I want the progress dialog to dismiss and start a new activity intent.
I know AsyncTask is the way to go. On onPreExecute method I show the progress dialog. On doInBackground I am performing the network operation. And onPostExecute I am dismissing the dialog and starting a new activity intent.
My issue is doInBackground will perform loopJ networkcall asynchronously so onPostExecute will finish first before my network operation. If you look at my logs it will show:
"Starting new activity!"
"Fetched category services!"
rather
"Fetched category services!"
"Starting new activity!"
How do I accommodate an asynchronous task running doInBackground? Is there a way in onPostExecute to wait till my asynch loopJ operation is done?
public class FetchCategoriesServices extends AsyncTask<HITECategory, String, String>
{
private Category userSelectedCategory;
private ProgressDialog busyDialog;
#Override
protected void onPreExecute()
{
busyDialog = ProgressDialog.show(SearchActivity.this, getApplicationContext().getString(R.string.progressDialogTitle),
getApplicationContext().getString(R.string.progressDialogMessage));
}
#Override
protected String doInBackground(HITECategory... params)
{
userSelectedCategory = params[0];
String requestCategoryServiceURL = BASE_URL + "GetServices?CategoryID=" + userSelectedCategory.categoryID + "&ResponseType=JSON";
try
{
Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler()
{
#Override
public void onSuccess(String jsonResponse)
{
Gson gson = new Gson();
CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class);
categoryServiceresults = Response.categoryServices;
Log.d(getString(R.string.DebugKey), "Fetched category services!");
}
});
}
catch (Exception e)
{
Log.d(getString(R.string.DebugKey), "Error connecting to service and fetching category services list");
}
return null;
}
#Override
protected void onPostExecute(String params)
{
busyDialog.dismiss();
Log.d(getString(R.string.DebugKey), "Starting new activity!");
Intent intent = new Intent(getApplicationContext(), CategoriesSearchActivity.class);
startActivity(intent);
}
}
Just put the code in onPostExecute into onSuccess method:
Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler()
{
#Override
public void onSuccess(String jsonResponse)
{
Gson gson = new Gson();
CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class);
categoryServiceresults = Response.categoryServices;
Log.d(getString(R.string.DebugKey), "Fetched category services!");
youractivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
busyDialog.dismiss();
Log.d(getString(R.string.DebugKey),
"Starting new activity!");
Intent intent = new Intent(getApplicationContext(),
CategoriesSearchActivity.class);
youractivity.this.startActivity(intent);
}
});
}
});