I have an app that is included a refresh button in actionbar. When I click on it for second time, my app goes to debug view and it says my asyncClass object isn't finished yet !!!
Please let me know how can I fix my problem.
MyActivity:
public class MyActivity extends ActionBarActivity {
private getAllDataFromServer asyncClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
asyncClass = new getAllDataFromServer();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == R.id.action_update)
if(asyncClass.getStatus() == Status.FINISHED || asyncClass.getStatus() == Status.RUNNING)
{
asyncClass.cancel(true);
}
if(asyncClass.getStatus() == Status.PENDING)
{
asyncClass.execute();
}
}
return super.onOptionsItemSelected(item);
}
getAllDataFromServer:
private class getAllDataFromServer extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(SendCommentsHistoryActivity.this);
pDialog.setMessage("Be patient ...");
pDialog.setCancelable(false);
pDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
if(isCancelled()) return null;
jObject = jParser.getJSONFromUrl(TAG_URL, param);
Log.i("JSONNNNNNNNN", jObject.toString());
try {
// get json array
for (int i = 0; i < jArray.length(); i++)
{
// get jsonObjects and fill database
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
fillListView();
pDialog.dismiss();
super.onPostExecute(result);
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}
}
UPDATE: JSONParser class:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
/**
* Send POST Data to Server & Retrieve JSON From URL
*/
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jObj;
}
}
Error screen shot:
Any suggestion will be appreciated ...