I've been working on an android app which regularly checks a mysql database using JSON and everything works fine with my code.
Im having trouble running this as a timer as it only runs once and then stops.
The only code i managed to get working runs the http request on the UI thread which freezes up.
Any help would be most appreciated.
Thank in advance,
#Override
protected void onCreate(Bundle savedInstanceState) {
...
checkUpdate.start();
...
}
private Thread checkUpdate = new Thread() {
public void run() {
try {
// my code here to get web request to return json string
}
String response = httpclient.execute(httppost, responseHandler);
mHandler.post(showUpdate);
}
...
}
private Runnable showUpdate = new Runnable(){
public void run(){
try{
// my code here handles json string as i need it
Toast.makeText(MainActivity.this,"New Job Received...", Toast.LENGTH_LONG).show();
showja();
}
}
}
private void showja(){
Intent i = new Intent(this, JobAward.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
As #Raghunandan suggested, the standard way to perform work in the background on Android, and then modify the UI when that work is done, is using AsyncTask.
First define a new subclass of AsyncTask:
private class JsonRequestTask extends AsyncTask<HttpUriRequest, Void, String> {
protected String doInBackground(HttpUriRequest... requests) {
// this code assumes you only make one request at a time, but
// you can easily extend the code to make multiple requests per
// doInBackground() invocation:
HttpUriRequest request = requests[0];
// my code here to get web request to return json string
String response = httpclient.execute(request, responseHandler);
return response;
}
protected void onPostExecute(String jsonResponse) {
// my code here handles json string as i need it
Toast.makeText(MainActivity.this, "New Job Received...", Toast.LENGTH_LONG).show();
showja();
}
}
and then you would use the task like this, instead of your Thread:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
JsonRequestTask task = new JsonRequestTask();
task.execute(httppost);
...
}
You may run the task again by simply creating a new JsonRequestTask() and calling its execute() method.
A common practice for a simple async task like this is to make it a private inner class within the Activity class that uses it (if only one Activity needs it). You may need to change the scope of some of your activity's variables so that the inner class may use them (e.g. move local variables to member variables).
Related
i've a little problem with my code.
I'm trying to perform a volley request on doInBackground behaviour and then to use its result to set some layout elements. Here's the async snippet:
private class MyAsyncTask extends AsyncTask<String, Void, Message>
{
#Override
protected Message doInBackground(String... params) {
rm = new RequestManager(MessageDisplay.this);
idM = params[0];
retrieveMessage(new VolleyCallbackOp(){
public void onSuccess(List<Message> ml) {
m = ml.get(0);
Log.d("print1", m.getContent()); <---- this is fine
}
});
Log.d("print2", m.getContent()); <---- m always null
return m;
}
#Override
protected void onPostExecute(Message m) {
super.onPostExecute(m);
Log.d("messaggio", m.getContent());
mds = new MessageDisplaySetter(MessageDisplay.this);
View v = mds.setMessageDisplayInfo(m, 3);
contentD.addView(v);
}
}
});
}
this code is called in activity onCreate():
Message m = new Message();
String target2 = i.getExtras().getString("messageId");
new MyAsyncTask().execute(target2);
The problem is that the Log "print2" tell the variable m is null while "print1" is ok.
Have you any tip to pass the result of this request to the related onPostExecute()?
The reason why your log for print2 is null - is because you are in a race condition. By calling new VolleyCallbackOp you are doing an asynchrounous network call which might take a while to complete. Your code does not wait until it's finished. That's why print2 is null most of the time.
There are several solutions for your case. One of them is to use the callback mechanism of onSuccess and do your logic there.
I am trying to trigger a HTTP post request on each button click to get some data from a webservice in android. I have created an async task to send the http request. My code is as follows :
Activity
public void sendHttpRequest(View view){
//gets called on button click
new MyHttpRequestTask(this).execute();
}
MyHttpRequestTask
public class MyHttpRequestTaskextends AsyncTask<Void, Void, Void> {
#Override
protected void onPostExecute(Void result) {
//Show received data
super.onPostExecute(result);
}
#Override
protected Void doInBackground(Void... arg0) {
//Send http request
return null;
}
}
I am having two problems, one is that the onPostExecute method is not being fired and the other is, if I press the button second time or multiple times, the task is not being executed. But somehow I think that onPostExecute method not being called is the reason for the task not being executed on second time. So, what am I doing wrong? How can I get rid of these issues? Thanks
Here is the full doInBackground method
#Override
protected Void doInBackground(Void... arg0) {
Looper.prepare();
GpsHelper gpsHelper = new GpsHelper();
LocationHelper locationHelper = new LocationHelper(mContext);
gpsHelper.turnGPSOn();
String location = locationHelper.getMyCurrentLocation();
...
String rawHtml = HttpHelper.sendPostRequest(postUrl, postParams);
HtmlHelper.processRawHtml(rawHtml);
Looper.loop();
return null;
}
It gets the current location using GPS, then the current address and posts them to a webservice, then the response from webservice is parsed and processed. Is the problem due to the GPS ?
Here you should call your method like
public MyHttpRequestTask myHttpRequestTask;
public void sendHttpRequest(View view){
//gets called on button click
//Status.PENDING -- got the status when your asynch task not run yet with same instance
//Status.RUNNING -- whether it running
if(myHttpRequestTask.getStatus() == Status.FINISHED || myHttpRequestTask.getStatus() == Status.PENDING){
myHttpRequestTask = new MyHttpRequestTask(this).execute();
}
}
if your asynch task is in running task then no need to start it again and it would not be start.
it would be feasible if you create a new object when your status of asynchtask is either finish or pending. and please put the log or system out in your post method so you can easily identify it whether it will be called. please check again.
Please do this in your code,
public void sendHttpRequest(View view){
//gets called on button click
new MyHttpRequestTask().execute();
}
public class MyHttpRequestTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
//Send http request
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//please put your code here.
}
}
try this code, and if any issues then please mention here.
After couple of days of head banging and debugging, I finally figured it out.
The onPostExecute was not being executed because of the GPS codes. Apparently, starting gps started few threads which were not terminated and hence onPostExecute was not being executed.
I am trying to get along with AsyncTask..
what my problem was that i was building a table of textviews dynamically, based on the output of a procedure.. But then i figured that by using asynctask i can do it in a more efficient way..So, what i did is as follows:
private class DisplayReport extends AsyncTask<Void, Void, Boolean>{
protected void onPreExecute(){
//Message -- "Please wait while the Report Loads..."
}
#Override
protected Boolean doInBackground(Void... params) {
//Here i fetch the data from the procedure via a web service
//parse the result of web service and set a bool variable true or false based on whether the dataset fetched is empty or not.
}
protected void onPostExecute(Boolean value){
if(value == true){
"Please try again later!!"
}
else{
runOnUiThread(GenTable);
}
}
private Runnable GenTable = new Runnable(){
public void run(){
try {
displayReport(result); // in this method i build the table.
} catch (Exception e) {
ad.setTitle("Error..");
ad.setMessage(e.toString());
}
}
};
}
the above async class is an inner class in my main class which extends activity.
and this is how i am executing the asynctask..
DisplayReport dr = new DisplayReport();
dr.execute();
now when i debug, i get the "source not found" error on dr.execute()..
i tried searching a lot on the net, but i simply cannot find out anything concrete. Also, please let me know if my approach is incorrect..
this question might seem very silly, but i am new to android and java as well and any help would be really great..
Thanks!
Execute is going to start a new thread. You don't want to debug into it. Instead, put breakpoints in onPreExecute, doInBackground, and onPostExecute and you can see when each of those gets called.
onPostExecute is already being run in the UI thread, so you should not create another runnable for it. Just make your onPostExecute like this:
protected void onPostExecute(Boolean value){
if(value == true){
String message = "Please try again later!!";
// Do something here with your message
}
else{
displayReport(result);
}
}
The goal:
Using Google App Engine server and Android client, I'm trying to put on the Google map at the Android client Users overlays. Every 30 seconds I'm polling the server and getting Vector that contains users and adding it to the map.
Current status:
I'm dong all that using in one new thread, So after running the app I got:
weird behaviors(delayed overlays, multiple overlays) and after that crushed with ConcurrentModificationException.
After reading a bit i figured out that I need to work with AsyncTask.
Correct me if I'm wrong,But I understand that everything done in the Activity at at onCreate is "running" in UIhread so I need to put the "Logic" (All the Network handling) in doInBackground and all the UI Handling like putting overlays on the map in onPostExecute.
My Question are:
1) In the current status I'm doing:
new Thread()
{
#Override
public void run()
{
super.run();
while(true)
{
SystemClock.sleep(30000);
Vector responseFromServer = getUsersVectorFromServer();
putNewOnlineUserOnTheMap();
}
}
}.start();
What is the right way to convert this To AsyncTask?
Do I poll the server still using new thread in the doInBackground or there is right way to do this?
2) Is there a specific list of what counts as UI to put in onPostExecute or any concepts list?
In my case I guess that in need to put putNewOnlineUserOnTheMap() in onPostExecute.
Thanks.
Something similar to the following:
class UpdateTask extends AsyncTask<Void, Vector, Void>{
#Override
protected Void doInBackground(Void... params) {
// this is running in a background thread.
while (!isCancelled()) {
SystemClock.sleep(30000);
Vector responseFromServer = getUsersVectorFromServer();
// send the result back to the UI thread
// onProgressUpdate will be called then
publishProgress(responseFromServer);
}
return null;
}
#Override
protected void onProgressUpdate(Vector... values) {
// this is executed on the UI thread where we can safely touch UI stuff
putNewOnlineUserOnTheMap(values[0]);
}
}
You can't use the result of the task since the task is finished then. But you can use the progress publishing mechanism to get periodic results. If you use it like that and do the modification on the UI thread you should not get ConcurrentModificationException because you do the modifications on the one thread that can safely modify the UI.
One thing to note here: create new instances of your Vector in the background thread and then use it to update the UI. But don't touch the same object afterwards in the backgroundthread. That way you don't need any synchronization since after the background thread sends it away it is only the UI thread that touches it. (and you could use a simple ArrayList instead of a Vector)
AsyncTask uses generics and varargs.The parameters that are passed to the asyntask are . TypeOfVariableArgumentsParameters is passed into the doInBackground(), ProgressParam is used for progress information and ResultParam must be returned from doInBackground() and is passed to onPostExecute() as parameter.
example:--
protected class ParsingTask extends AsyncTask> {
private ProgressDialog loadingDialog = new ProgressDialog(JsonParserActivity.this);
protected void onPreExecute() {
loadingDialog.setMessage("loading app store..");
loadingDialog.show();
}
#Override
protected ArrayList<Items> doInBackground( Context... params ) {
// do ur process here.
return result;
}
if (!this.isCancelled()) {
}
return result;
}
#Override
protected void onProgressUpdate(String... s) {
super.onProgressUpdate(s);
Toast.makeText(getApplicationContext(), s[0], Toast.LENGTH_SHORT).show();
}
#Override
protected void onPostExecute( ArrayList<Items> response ) {
//if u r dealing with list view and adapters set the adapter here at the onPostExecute()
loadingDialog.dismiss();
}
#Override
protected void onCancelled() {
super.onCancelled();
Toast.makeText(getApplicationContext(), "The operation was cancelled", 1).show();
}
}
You can use AsyncTask like below. Hope this will help you..
Class YourClass{
void YourClass(){
NetworkTask nT = new NetworkTasK();
nT.execute();
}
}
protected class NetworkTask extends AsyncTask<Void, String, Boolean>
{
#Override
protected Boolean doInBackground(Void... params)
{
try
{
String response;
while(keepreceiving)
{
response = in.readLine();//Prog Counter stops here until getting i/p.
if(response != null)
yourFunctionForResponse(response);
}
}
catch (Exception ex)
{
}
return null;
}
private void yourFunctionForResponse(String response){
//things to do....
}
}
You may also try runOnUiThread(Runnable action) along with this to implement your work.
I have an application that is performing HTTP Requests (specifically calling the FogBugz API) when the user clicks certain buttons. Right now, I am just creating a service when the application starts, and then calling different methods in that service to complete requests. However, when I do this, there is the usual hang in the UI thread. I have looked at AsyncTask, but am not sure it will do what I want to accomplish. Because I need to instantly parse the XML that the HTTP Request returns, I need to have a process that is able to return this data to the UI thread. Will ASyncTask be able to accomplish this, or is there some other way?
public static InputStream makeRequest(String httpRequest)
{
In a separate thread, run HTTP Request, get back and process, return inputstream
}
This method is called by several others to perform HttpRequests. Once the inputstream is returned, the other methods parse for specific information.
The simplest way to do it would just be to do something like
//Body of your click handler
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
//code to do the HTTP request
}
});
thread.start();
That will cause the code inside the run() method to execute in a new thread of execution. You can look into an async task if you like although I've personally never used it. This is a quick and simple way to get things done.
With regards to passing information back, I would use a Handler object which effectively allows you to set up a message queue for a given thread and pass messages to it which cause the execution of specific code. The reason you need to do this is because Android will not let any thread other than the UI thread update the actual UI.
Does that address your question? I know my first pass didn't fully address all of your issues.
Edit Basically, what you do is define a handler object in your Activity like
private Handler handler_ = new Handler(){
#Override
public void handleMessage(Message msg){
}
};
You also create static int constants that help tell the handler what to do. You basically use those to allow for several different types of messages to be passed to one instance of a handler. If there is only going to be one message that is passed back, then you don't have to worry about that.
For example
private static final int UPDATE_UI = 1;
To send a message to the handler you call
handler_.sendMessage(Message.obtain(handler_, UPDATE_UI, inputStreamInstance));
From the handler:
private Handler handler_ = new Handler(){
#Override
public void handleMessage(Message msg){
switch(msg.what){
case UPDATE_UI:
InputStream is = (InputStream)msg.obj;
//do what you need to with the InputStream
break;
}
}
};
Alternatively, where the inputStreamInstance is added to the Message object, you can pass any object you like so if you wanted to parse it into some kind of container object or something like that, you could do that as well. Just cast it back to that object from within the handler.
Try using AsyncTask. Goto this Link for more:
private class SyncIncoData extends AsyncTask<String, String, String> {
ProgressBar pb;
ProgressBar pbar;
#Override
protected String doInBackground(String... urls) {
for (int i = 0; i <= 100; i++) {
publishProgress(i);
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pb = (ProgressBar) findViewById(R.id.progressBarsync4);
pb.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(String result) {
pb = (ProgressBar) findViewById(R.id.progressBarsync4);
pb.setVisibility(View.INVISIBLE);
}
#Override
protected void onProgressUpdate(String... values) {
pbar = (ProgressBar) findViewById(R.id.progressBarpbar);
pbar.setProgress(Integer.parseInt(values[0]));
super.onProgressUpdate(values);
}
}
Write the program for http request inside doinbackgroud()