App crash when execute asynctask - android

my app crash when execute asynctask. I try to send data from android to the server but my app crashes when execute AsyncTask
` input input = new input();
input.execute();
mainActivity = this;
latitude = "-6.711647";
longitude ="108.5413";`
public class input extends AsyncTask<String, String, String>
{
HashMap<String, String> user = db.getUserDetails();
String email = user.get("email");
String success;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Sending Data to server...");
pDialog.setIndeterminate(false);
pDialog.show();
}
#Override
protected String doInBackground(String... arg0) {
String strEMAIL = email.toString();
String strNama = latitude.toString();
String strProdi = longitude.toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", strEMAIL));
params.add(new BasicNameValuePair("latitude", strNama));
params.add(new BasicNameValuePair("longitude", strProdi));
JSONObject json = jParser.makeHttpRequest(url, "POST", params);
try {
success = json.getString("success");
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_LONG).show();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
if (success.equals("1"))
{
Toast.makeText(getApplicationContext(), "kirim data Sukses!!!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "kirim data Gagal!!!", Toast.LENGTH_LONG).show();
}
}
}
this my complete code http://pastebin.com/jRdxeQKG
and this my logcat http://prntscr.com/7p3vbz

The reference db is null when you call new input() in onCreate().
Solution: Just initialize db before calling new input():
db = new SQLiteHandler(getApplicationContext());
input input = new input();
By the way, there are many issues from the design point of view, but this will solve (one of) the crash(es).

1 Starting the name of your class with a lowercase letter is bad practice.input should be Input and should be a little more descriptive.
2 Your db initialization should come before the call to execute AsyncTask
3 You need to really understand AsyncTask before using it. You declare three generic types for your AsyncTask
public class input extends AsyncTask<String, String, String> {
but none of them is used. Your doInBackground method should be returning an instance of type NameValuePair or JSONObject.
The first generic type of an AsyncTask is the params, that is what you intend to pass to your doInBackground method. You say it's String but do not pass in any String in input.execute(); This is not a problem right now because you haven't attempted to use args0, but your app will crash if you try to. I just think you should really read about AsyncTask first and understand the basics.It will save you a whole lot of trouble.Cheers.

Related

when execute two AsyncTask in same fragment activity my application hanging and still loading

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.

How to refactor my code in AsyncTask without runOnUiThread?

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();
}
}

async data not getting cleared

I have 3 async tasks currently running Login, Registration, and GetInstitutes. I made the Login class Function first and i know it worked fine, however adding the other 2 has caused it not to work. So When the app launches it does GetInstitutes and returns
{"institute":[{"id":"1","name":"plymouh university"},{"id":"2","name":"University Of Bradford"},{"id":"3","name":"University of West England"}],"success":1}
if i register it returns:
{"message":"Invalid email","success":0}
These run fine and get there JSON Objects fine, however when Login instead of getting the JSON object for login i get one of the previous Logins
class loginAttempt extends AsyncTask<String, String, String>{
JSONObject json = null; //reseting the JSON Object to null to try and fix
#Override
protected void onPreExecute() {
//Show progress Dialog
}
#Override
protected String doInBackground(String... params) {
String usernameSTR = username.getText().toString();
String passwordSTR = password.getText().toString();
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("username", usernameSTR));
params1.add(new BasicNameValuePair("password", passwordSTR));
Log.d("Create Response", json.toString()); //outputs null
// getting JSON Object
json = jsonParser.makeHttpRequest(url_login,
"POST", params1); //Performs Login request
Log.d("Create Response", json.toString()); //outputs original JSON from register or Get Institute not from the JSON above
//manipulate results
return null;
}
protected void onPostExecute(String file_url) {
//Dismiss Dialouge and show results
}
}

Android Async doinbackaground data to onpostexecute

I need some help with taking results from doinbackground to onpostexecute. The results are in the form of JSONArray. I want to populate two textviews in the UI through onpostexecute. There is no error in the code but nothing happens as it seems inpostexecute is not getting called. Please help. I have looked at several similar questions here and tried many things but unable to get it to work..thanks in advance. Please note private static final String TAG_LATEST_SCORES = "cricket_scores";
class PostScores extends AsyncTask<JSONArray, Void, JSONArray> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewScoreupdatescricketActivity.this);
pDialog.setMessage("Posting LiveScore Updates..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected JSONArray doInBackground(JSONArray... args) {
String runs1 = runs.getText().toString();
String wicket1 = wicket.getText().toString();
String overs1 = over.getText().toString();
String rr1 = rr.getText().toString();
String team_name = null;
String bat1;
if (innings.isChecked() == false) {
bat1 = "1";
team_name = bat.getText().toString();
} else {
bat1 = "2";
team_name = bat.getText().toString();
}
if (rr1.equals("NA")) {
rr1 = "0";
}
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("runs", runs1));
params.add(new BasicNameValuePair("wickets", wicket1));
params.add(new BasicNameValuePair("id", id1));
params.add(new BasicNameValuePair("overs", overs1));
params.add(new BasicNameValuePair("rr", rr1));
params.add(new BasicNameValuePair("bat", bat1));
params.add(new BasicNameValuePair("team_name", team_name));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
pDialog.dismiss();
if (success == 1) {
latest_scores = json.getJSONArray(TAG_LATEST_SCORES);
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
return latest_scores;
}
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(JSONArray latest_sc) {
// dismiss the dialog once done
try {
for (int i = 0; i < latest_sc.length(); i++) {
JSONObject c = latest_sc.getJSONObject(i);
String wickets = c.getString(TAG_WICKETS);
String runs = c.getString(TAG_RUNS);
score_inngs1.setText(wickets + runs);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Check your brackets. After your definition of doInBackground, you have an extra bracket - that one closes off your PostScores class, so your onPostExecute method is defined as a member of whatever class is enclosing PostScores, if any.
Also, you'll need to #Override the AsyncTask methods to provide your own implementation.

error in AsyncTask class unable to send data

I am working in android platform and want to send some signup details to a php page for that i created an activity named as 'ContinueRegister' it contains an AsyncTask class. while running my project it shows unfortunately has stopped and there is some errors in logcat like
Activity com.opz.ContinueRegister has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#41269db8
error opening trace file: No such file or directory
at android.os.Looper.loop(Looper.java:137)
at android.os.Handler.handleCallback(Handler.java:615)
atcom.opz.ContinueRegister$CreateNewUser.onPreExecute(ContinueRegister.java:76)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at java.lang.reflect.Method.invokeNative(Native Method)
this is my class file
public class ContinueRegister extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText firstname;
EditText lastname;
EditText dob;
EditText gender;
RegisterActivity ra= new RegisterActivity();
// url to create new product
private static String url_create_product = "http://localhost/login_api/create_account.php/";
// JSON Node names
private static final String TAG_SUCCESS = "success";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set View to register.xml
setContentView(R.layout.registerfinsh);
Button bt=(Button)findViewById(R.id.btnRegister);
// Listening to Login Screen link
bt.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
new CreateNewUser().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ContinueRegister.this);
pDialog.setMessage("Creating New User..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating User
* */
protected String doInBackground(String... args) {
String Username = ra.username.getText().toString();
String Email = ra.email.getText().toString();
String Password =ra.password.getText().toString();
firstname = (EditText)findViewById(R.id.first_name);
lastname =(EditText)findViewById(R.id.last_name);
dob =(EditText)findViewById(R.id.date_of_birth);
gender = (EditText)findViewById(R.id.gender);
String Firstname = firstname.getText().toString();
String Lastname = lastname.getText().toString();
String Dob =dob.getText().toString();
String Gender =gender.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", Username));
params.add(new BasicNameValuePair("Email", Email));
params.add(new BasicNameValuePair("Password", Password));
params.add(new BasicNameValuePair("Firstname", Firstname));
params.add(new BasicNameValuePair("Lastname", Lastname));
params.add(new BasicNameValuePair("Dob", Dob));
params.add(new BasicNameValuePair("Gender", Gender));
// getting JSON Object
// Note that create user url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created user
Intent i = new Intent(getApplicationContext(), FinishSignupActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create user
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
TextView loginScreen = (TextView) findViewById(R.id.link_to_login2);
loginScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent m = new Intent(getApplicationContext(),LoginActivity.class);
startActivity(m);
}
});
}
}
}
please help me to fix this errors
String url_create_product = "http://10.0.2.2/login_api/create_account.php/";
OR
Use your local IP Like 192.168.0.1
String url_create_product = "http://192.168.0.1/login_api/create_account.php/";
Also check your login_api folder have file which name is create_account.php
One more thing is Never Access Any view inside doinBackGround(...)
Because doinBackGround method is non-UI thread.
we must give complete url like 192.10.1.000.
you can check in windows by using localhost.but you must provide complete url in the above line.
To know ur ipaddress open cmd prompt. and type ipconfig.
Put the code for calling intent onPostexecute
going to another activity is never a background task

Categories

Resources