I have been using this code for the last few days and up until today it has been working perfectly, but for some reason the Async task has stopped calling the doInBackground method. I have attempted the solution suggested here Android SDK AsyncTask doInBackground not running (subclass) but I just get the same result. The onPreExecute method gets called but then I am just left with the loading dialog. Has anybody experienced anything similar to this. I have included a copy of my code. MyAsncTask is executed as follows;
new MyAsyncTask().execute(email, password);
private class MyAsyncTask extends AsyncTask<String, Void, JSONObject>
{
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
Log.v(tag, "onPreExecute");
super.onPreExecute();
pDialog = new ProgressDialog(SignInPageActivity.this);
pDialog.setMessage("Logging in...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... params) {
Log.v(tag, "doInBackground");
CustomerFunctions userFunction = new CustomerFunctions();
if (params.length != 2)
return null;
JSONObject json = userFunction.loginUser(params[0], params[1]);
return json;
}
#Override
protected void onPostExecute(JSONObject json)
{
Log.v(tag, "onPostExecute");
try {
if (json != null && json.getString(KEY_SUCCESS) != null) {
signInError.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler databaseHandler = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("customer");
// Clear all previous data in database
CustomerFunctions userFunction = new CustomerFunctions();
userFunction.logoutCustomer(getApplicationContext());
databaseHandler.addUser(json.getString(KEY_UID), json_user.getString(KEY_FIRST_NAME), json_user.getString(KEY_LAST_NAME), json_user.getString(KEY_EMAIL), json_user.getString(KEY_CURRENT_STAMPS), json_user.getString(KEY_TOTAL_STAMPS), json_user.getString(KEY_REWARDS_AVAILABLE), json_user.getString(KEY_REWARDS_CLAIMED), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent main = new Intent(getApplicationContext(), MainActivity.class);
// Close all views before launching Dashboard
// dismiss the dialog once done
pDialog.dismiss();
main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
// Close Registration Screen
finish();
}else{
// Error in login
signInError.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
try extending like this
private class MyAsyncTask<Params, Void, JSONObject> extends AsyncTask<Params, Void, JSONObject>
and use #Override for doInBackground
Related
I am trying to execute two AsyncTask in the same fragment but when I run my application, my phone freezes and still loading this my code
when I execute one asynTask every thing is ok
this where it try to execute
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.w(LOG_TAG,"onActivityCreated------------");
// First AsyncTask execution called
new RecomenddedChaneel().execute();
//Second AsyncTask execution called
new Loadchannels().execute();
//this is the first asyncTask method
public class RecomenddedChaneel extends AsyncTask<String, Void, List<Video>> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading channel list. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected List<Video> doInBackground(String... urls) {
channelForCategory= new ArrayList<Video>();
channelForCategory.clear();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uky", uk));
params.add(new BasicNameValuePair("per_page","10"));
JSONObject json = JsonFromHttp.makeHttpRequest(url+"getrecommended_top","POST", params);
try {
products=json.getJSONArray("products");
channelForCategory.clear();
listchD.clear();
String imageViewch =null;
for(int i =0; i <products.length();i++){
JSONObject details = products.getJSONObject(i);
String pid =details.getString("pid");
String cid=details.getString("cid");
String name= details.getString("name");
Log.v("",name);
if(details.has("imageViewch")){
imageViewch = details.getString("imageViewch");}
String image_url_path= imageurl+imageViewch;
channelForCategory.add(new Video(pid,cid,name,image_url_path));
listchD.add(new Video(name,pid));
Log.w(LOG_TAG,"pid*************all************"+pid);
Log.w(LOG_TAG,"cid*********all**********"+cid);
Log.w(LOG_TAG,"name********all**********"+name);
Log.w(LOG_TAG,"imagelink*********all***********"+imageViewch);
}
} catch (JSONException e){
Log.e("SetHttp", "Problem parsing News JSON results", e);
}
return null;
// return videos;
#Override
protected void onPostExecute(List<Video> video) {
pDialog.dismiss();
ChannelRecycleViewAdapter channelRecycleViewAdapter1
= new ChannelRecycleViewAdapter(getActivity(), channelForCategory, new ChannelRecycleViewAdapter.ListItemClickListener() {
#Override
public void onListItemClick(int clickedItemIndex) {
Video videoch= channelForCategory.get(clickedItemIndex);
String urlch=videoch.getmpid();
Intent inturl= new Intent(getActivity(),VideoPlayer.class);
inturl.putExtra("urlch",urlch);
inturl.putExtra("list_video_name", (Serializable) listchD);
startActivity(inturl);
Toast.makeText(getActivity(),
urlch, Toast.LENGTH_LONG).show();
}
});
recyclerView1.setAdapter(channelRecycleViewAdapter1);
and the second asynctask is the copy of the first asynctask.
I don't know why app hanging and still loading.
i am trying to parse a jsonObject, but i cant get the result out of doInBackground into onPostExecute
Here is my AsyncTask code:
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String auth2 = jsonObj.getString("auth");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
#Override
protected void onPostExecute(Void auth2) {
super.onPostExecute(auth2);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
Toast.makeText(getApplicationContext(), "String retrived:" + auth2, Toast.LENGTH_SHORT).show();
}
}
I know its propably because i have return null there, but when i make return string then i get error.
I know in fact that jsonStr holds json data, i can see it in log:
Response:﹕ > {"user_info":{"auth":0}}
I put this code together from tutorials, thats why i dont completly understand it.
My goal is to see if auth is 0 or 1.
cant get the result out of doInBackground into onPostExecute
To return auth2 String from doInBackground :
1. Change return type of doInBackground method from Void to String:
#Override
protected String doInBackground(Void... arg0) {
}
2. Change AsyncTask last generic type from Void to String :
private class GetContacts extends AsyncTask<Void, Void, String>
3. Return auth2 from doInBackground :
String auth2 = jsonObj.getString("auth");
return auth2;
4. Change onPostExecute parameter type from Void to String :
#Override
protected void onPostExecute(String auth2) {
super.onPostExecute(auth2);
//...
Toast.makeText(getApplicationContext(),
"String retrived:" + auth2, Toast.LENGTH_SHORT).show();
}
read the documentation:
http://developer.android.com/reference/android/os/AsyncTask.html
private class GetContacts extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... arg0) {
...
return "askdjalskdj";
}
#Override
protected void onPostExecute(String auth2) {
Log.i("Output", auth2);
}
}
See the params I have set in the Generic implementation of Asynctask , see the defined return value from doInBackground and the Parameter type of onPostExecute
AsyncTask's generic types The three types used by an asynchronous task are the following:
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the
background computation.
Result, the type of the result of the
background computation.
Not all types are always used by an
asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask { ... }
Hello android developers! I have one problem with compatibility of my old codes and API 18.
All codes bellow runs perfectly in android 2.3. But when I try to run it on a newer API it crashes. Can I use it without runOnUiThread for updating my UI? What I shoud change? Thanks!
class GetDetails extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(QueryProduct.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
tvName = (TextView) findViewById(R.id.tvName);
tvName.setText(product.getString(TAG_NAME));
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
Can I use it without runOnUiThread for updating my UI?
YES! Please do! Remove runOnUiThread() and return your result to onPostExecute() and update UI there. That's why the other methods exist.
The way you have it, you are running all of your background stuff on the UI which defeats the purpose of AsyncTask.
You can simply change your class definition to pass the int to onPostExecute()
class GetDetails extends AsyncTask<String, String, Integer>
then return success in doInBackground(). And check that value in onPostExecute() and update UI if success.
protected void onPostExecute(Integer result)
{
if (result == 1)
{
// code to update UI
Further ExplanationBe sure to read through the AsyncTask Docs really well. This class was created so that you can do your heavy-lifting such as network stuff on a background Thread (doInBackground()) and update the UI in it's built-in methods (the other 3).
onPreExecute() can update before the task starts such as for a
ProgressDialg
onProgressUpdate() can update while doInBackground() is still processing such as for the progress of a downloading file by calling publishProgress()
onPostExecute() is called when doInBackground() finishes to
update anything you need when the task finishes such as dismissing a
ProgressDialog
class GetDetails extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(QueryProduct.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
// Do your Background Task Inside it and inside it when you want to update the Ui
// then call the method publishProgress. this call the method onProgressUpdate that
// run on the UI thread and inside it you update your UI
protected String doInBackground(String... params) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
String result = product.getString(tvName)
// Need To Update the Ui so call the method publishProgress(result );
// this will call onProgressUpdate(String... values) method and
// String result i provide it to so that i can update the text view
// in it.
//Update in publish progress you can specify multiple String values
// and same received in onProgressUpdate as an array of values.
//publishProgress("value1","value2","value3")
publishProgress(result,"result2","result3");
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
// run on the Ui thread so inside it you can update your UI
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
tvName = (TextView) findViewById(R.id.tvName);
tvName.setText(values[0]);
tvPrice = (TextView) findViewById(R.id.tvPrice);
tvPrice.setText(values[1]);
tvAdress = (TextView) findViewById(R.id.tvAdress);
tvAdress .setText(values[2]);
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
I am trying to create an activity that allows a user to login by checking the username and password from a database, however after the creadentials are fetched successfully, the doInbackground doesnt stop executing.I not sure what i can do to make the onpostexecute to run. Here is the code
public class LoginActivity extends Activity{
public String username;
public String password;
public String userid;
JSONParser jParser = new JSONParser();
JSONObject json;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
context=getApplicationContext();
setContentView(R.layout.activity_login);
Button loginbutton=(Button) findViewById(R.id.loginbutton);
final EditText usernameText=(EditText) findViewById(R.id.usernameInput);
final EditText passwordText=(EditText) findViewById(R.id.passwordInput);
loginbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
username=usernameText.getText().toString();
password=passwordText.getText().toString();
if(username.trim().length()==0 || password.trim().length()==0){
AlertDialogManager diag=new AlertDialogManager();
diag.showAlertDialog(getApplicationContext(), "Fill Fields", "enter a username and password", false);
}else{
//send the username and password for verification
new Login().execute();
}
}
});
}
//http class starts here.
class Login extends AsyncTask<String, String, String> {
InputStream is = null;
JSONObject jObj = null;
ProgressDialog pDialog;
static final String url = "http://10.0.2.2/newptk/dalvik/auth.php";
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Authenticating...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
Log.e("Auth", "working");
JSONArray document = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
json = jParser.makeHttpRequest(url, "POST", params);
return null;
}
protected void onPostExecute() {
pDialog.dismiss();
SessionManager smg=new SessionManager(getApplicationContext());
int flag = 0;
try {
flag = json.getInt("success");
if(flag==1){
userid=json.getString("userid");
//set the session
smg.createLoginSession(username, userid);
//Login the user
Intent i = new Intent(getApplicationContext(), ReportFound.class);
startActivity(i);
finish();
}else{
AlertDialogManager diag=new AlertDialogManager();
diag.showAlertDialog(LoginActivity.this, "Login", "Incorrect Username/password", false);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}//end of http class
}
The first thing I see is that you are telling onPostExecute() to accept a String in your class header
class Login extends AsyncTask<String, String, String>
but aren't accepting anything or passing anything to it
protected void onPostExecute() {
if you don't want to pass it anything then change it to
class Login extends AsyncTask<Void, Void, Void>
and
protected void onPostExecute(Void result) {
...
}
#Override
protected void doInBackground(String... arg0) {
Notice this section in the Docs
The three types used by an asynchronous task are the following:
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the
background computation.
Result, the type of the result of the
background computation.
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
Change
protected void onPostExecute()
to
protected void onPostExecute(String result)
and you should be golden. Consider adding #Override tags in your code to prevent these subtle bugs in the future.
I try use below code to load an URL.
URL url = new URL(urlstr);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(10000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
InputStream is = connection.getInputStream(); //spend lots of time
Because the line InputStream is = connection.getInputStream(); will spend some time.
So I want to show a loading dialog while it loading.
I can I do it?
In AActivity, below code to call BActivity.
Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window w = MyGroup.group.getLocalActivityManager().startActivity("BActivity", intent);
View view = w.getDecorView();
MyGroup.group.setContentView(view);
And BActivity is load URL and extract information.
The load code is in onCreate().
I try the answer code, the error Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord#2afe9488 is not valid; is your activity running? shows.
You can achieve with the aysntask showing progress dialog as follows
In Oncreate :
new GetTask(this).execute();//taken object for asyntask class.
class GetTask extends AsyncTask<Object, Void, String> {
{
ProgressDialog progressDialog;
void GetTask(Context cntxt)
{
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mDialog = new ProgressDialog(cntxt); //taking object for progress dialog
mDialog.setMessage("Please wait...");
mDialog.show(); //Displaying progressDialog
}
#Override
protected String doInBackground(Object... params) {
//do the background process
return ""; you can return string value
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (mDialog != null) {
mDialog.dismiss();//close the progress Dialog
}
}
}
You want a ProgressDialog. Refer to this link
use a constructor in DownloadWebPageTask to initialize the context and use that context in dialog.
or use yourclass.this in
dialog = new ProgressDialog(yourclass.this);
Progress dialog
private ProgressDialog dialog;
public void showProgress () {
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Please wait");
dialog.show();
}
Use asynchronous task for downloding...
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
//Do your downloading task
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
#Override
protected void onPostExecute(String result) {
dialog.dismiss();
}
}
Call progress dialog before executing download task
showProgress();
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.url.com" });