In webservice class, it will retrieve database from online and put into list:
public List<List_NewsComment> allCommentList;
public void GetCommentNews( final int gCommentNewsID)
{
Thread networkThread=new Thread(){
#Override
public void run(){
try
{
SoapObject request= new SoapObject(NAMESPACE,get_Comment_METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE androidHttpTransportSE= new HttpTransportSE(URL,60000);
request.addProperty("itemid", gCommentNewsID);
envelope.setOutputSoapObject(request);
androidHttpTransportSE.call(get_Comment_SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.bodyIn;
RetrieveFromSoap( result);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
networkThread.start();
}
public List<List_NewsComment> RetrieveFromSoap(SoapObject soap)
{
allCommentList= new ArrayList<List_NewsComment>();
Vector<Object> property2 = extracted(soap);
for (int i = 0; i< property2.size();i++){
SoapObject getPropertyD=(SoapObject)property2.get(i);
List_NewsComment addcomment= new List_NewsComment();
addcomment.setcommentDate(getPropertyD.getProperty("date").toString());
addcomment.setUserName( getPropertyD.getProperty("name").toString());
addcomment.setCommentContent(getPropertyD.getProperty("comment").toString());
allCommentList.add(addcomment);
}
webservice.allCommentList.size(); <-- can call here no problem
return allCommentList;
}
private static Vector<Object> extracted(SoapObject soap) {
return (Vector<Object>)soap.getProperty(0);
}
In Main Activity class, i want to check the size of list:
Database_WebService webservice = new Database_WebService(this);
webservice.GetCommentNews(newsid);
webservice.allCommentList.size(); <-- cannot call here, what is the problem
It returns me nullpointerexception.
What is the problem????
Make your List static so you can access it anywhere using that class name .
Like
public static List<List_NewsComment> allCommentList;
Database_WebService.allCommentList.size();
Or
As everyone suggest make one function in your class and return the size of array in that function. Please see code below.
public int getCommentListSize()
{
return allCommentList.size();
}
then call this method as
Database_WebService databaseWebservice = new Database_WebService(this);
int size =databaseWebservice.getCommentListSize.size();
The reason it is not initialized is because it is being initialized within the thread, so the sequence of events goes like this:
GetCommentNews starts
The thread starts
Now the thread and usage of list size (that's broken) is being run at the same time
You cannot use allCommentList until after the thread has finished execution, so you need to signal or do something with allCommentList within the RetrieveFromSoap method after it has been assigned.
Also you should not make the list static, this goes against the principles of object-oriented programming, and you should expose allCommentList through a getter not directly with a variable.
Edit:
You may actually be better off staying away from Thread and using an AsyncTask instead, this class was designed to make doing UI work from a Thread easier.
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Reference: AsyncTask | Android Developers
Because List allCommentList is not initialized there it will initialize inside the public List RetrieveFromSoap(SoapObject soap).
public static List<List_NewsComment> allCommentList;
int size =Database_WebService.allCommentList.size();
--------------------------------------------------
still i recommended don't not good to use static better to make one method
public int getArrSize()
{
return allCommentList.size();
}
then call this method as
Database_WebService dvws = new Database_WebService(this);
int size =dvws allCommentList.size();
You have to be careful as you are creating the List in a diferent thread (networkThread) from where are you calling it (from the main thread). You should be sure that networkThread has finished before accesing it from outside.
This is very ugly, why not add a method like 'int webservice.getCommentsSize()' and return the size, so you could test if the list is not empty and you don't need direct access.
like:
class Webservice{
private List allCommentList = null;
public int getCommentListSize(){
if(allCommentList==null) return 0;
return allCommentList.size();
}
}
in main:
Webservice webservice = new Webservice();
int size = webservice.getCommentListSize();
another problem is, that your list probably won't be ready at the time you try to get it's size. Why are you running this in a thread? Have you seen AsyncTask ?
Make the ArrayList static. Then you can get access using classname.YourArrayList from the other class.
Related
i have generated an app over e-commerce site (magento 2) while i am trying to startup my app it processing very slowly because of many products in my server is there any possible way to speed up my usage of Async task while using JSON feeds.. Please let me for any possible ways
My one of the AsyncTask coding:
private class GetProduct extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
dialog_pro = new ProgressDialog(Healthy_Cat.this);
dialog_pro.setMessage("Please wait...");
dialog_pro.setCancelable(false);
dialog_pro.show();
}
#Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
JSONArray items = new JSONArray(jsonStr);
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
pro_name = c.getString("name");
String price = c.getString("price");
JSONArray array = c.getJSONArray("custom_attributes");
for (int k = 0; k < array.length(); k++) {
JSONObject jb = array.getJSONObject(k);
String attr = jb.getString("attribute_code");
if (attr.equalsIgnoreCase("special_price")) {
splprice = jb.getString("value");
}
}
String sku = c.getString("sku");
JSONArray media = c.getJSONArray("media_gallery_entries");
for(int k = 0; k < media.length(); k++) {
JSONObject jb = media.getJSONObject(k);
String imageURL = BaseURL_Helper.MediaBase +jb.getString("file");
media_image = imageURL;
// tmp hash map for single contact
Beanclass dataSet = new Beanclass();
dataSet.setTitle(pro_name);
dataSet.setImage(imageURL);
dataSet.setPrice(price);
dataSet.setSPLPrice(splprice);
dataSet.setSku(sku);
list.add(dataSet);
BeanclassList data = new BeanclassList();
data.setTitle(pro_name);
data.setImage(imageURL);
data.setSku(sku);
data.setSPLPrice(splprice);
data.setPrice(price);
listbean.add(data);
}
}
}catch (final JSONException e) {
runOnUiThread(new Runnable() {
#Override
public void run() {
no_list.setVisibility(View.VISIBLE);
}
});
}
} else {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"May be Network error!!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
/**
* Updating parsed JSON data into ListView
* */
if (dialog_pro.isShowing())
dialog_pro.dismiss();
mAdapter = new GridviewAdapter(Healthy_Cat.this,list);
gridlist.setAdapter(mAdapter);
Listadapter = new ListviewAdapter(Healthy_Cat.this,listbean);
listview_pro.setAdapter(Listadapter);
}
}
Thank u in advance..
There are few things you need to update in your code
API Calling lib: I'm using Retrofit for api calling very fast & simple to use. Support Header & Response caching too.
JSON Parsing: You are parsing JSON manually which is a time-consuming process. I'm using Google's JSON parsing Lib Gson. really very fast.
Pagination: If you having lots of data on the server then try to fetch data in small no of pieces. For example in case of "Item Listing API" try to fetch data from the server for 10-15 item at a time rather all the item at a once.
Asyntask performance speed is too low compare to Retrofit.
So For e-commerce app, You must use Retrofit.
There are a few things you can do. For starters, you don't have to parse the whole json, before updating view.
But really you yourself state the issue, which is you have too much data. This is not only a programming issue, it is also a user experience issue, too much data is confusing, especially on a mobile device.
What I suggest is breaking down your data into categories or the like. When app starts, download just a list of categories to display. Upon user choosing a category, then you download the data for that category.
When you download data, do it in chunks, so that you can start displaying right away.
There are many similar ideas that you can implement, for a better user experience.
I have a doubt about the possibility of repeating an AsyncTask in an application for Android. I would like to repeat some operations, the download of a file from a server for example, n times if it is impossible for some reasons download the file. There is a quick way to do this?
You cannot repeat an AsyncTask but you could repeat the operations it executes.
I've made this little helper class that you might want to extend in place of AsyncTask, the only big difference is that you will use repeatInBackground instead of doInBackground and that onPostExecute will have a new parameter, the eventual Exception thrown.
Anything inside repeatInBackground will be repeated automatically until result is different from null / exception is not thrown and there are been less than maxTries.
The last exception thrown inside the loop will be returned in the onPostExecute(Result, Exception).
You can set max tries using the RepeatableAsyncTask(int retries) constructor.
public abstract class RepeatableAsyncTask<A, B, C> extends AsyncTask<A, B, C> {
private static final String TAG = "RepeatableAsyncTask";
public static final int DEFAULT_MAX_RETRY = 5;
private int mMaxRetries = DEFAULT_MAX_RETRY;
private Exception mException = null;
/**
* Default constructor
*/
public RepeatableAsyncTask() {
super();
}
/**
* Constructs an AsyncTask that will repeate itself for max Retries
* #param retries Max Retries.
*/
public RepeatableAsyncTask(int retries) {
super();
mMaxRetries = retries;
}
/**
* Will be repeated for max retries while the result is null or an exception is thrown.
* #param inputs Same as AsyncTask's
* #return Same as AsyncTask's
*/
protected abstract C repeatInBackground(A...inputs);
#Override
protected final C doInBackground(A...inputs) {
int tries = 0;
C result = null;
/* This is the main loop, repeatInBackground will be repeated until result will not be null */
while(tries++ < mMaxRetries && result == null) {
try {
result = repeatInBackground(inputs);
} catch (Exception exception) {
/* You might want to log the exception everytime, do it here. */
mException = exception;
}
}
return result;
}
/**
* Like onPostExecute but will return an eventual Exception
* #param c Result same as AsyncTask
* #param exception Exception thrown in the loop, even if the result is not null.
*/
protected abstract void onPostExecute(C c, Exception exception);
#Override
protected final void onPostExecute(C c) {
super.onPostExecute(c);
onPostExecute(c, mException);
}
}
You cannot reuse the same AsyncTask object as, according to the AsyncTask Docs
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
But you can create however many new objects of that class you need inside of a loop. However a better way you be to do the download operation n number of times inside your doInBackground().
If this doesn't answer your question then please be more specific as to your problem
I did it that way. It can try and try until (tries == MAX_RETRY) or the result is not null. A slightly modified code from accepted answer, better for me.
private class RssReaderTask extends AsyncTask<String, Void, ArrayList<RssItem>> {
// max number of tries when something is wrong
private static final int MAX_RETRY = 3;
#Override
protected ArrayList<RssItem> doInBackground(String... params) {
ArrayList<RssItem> result = null;
int tries = 0;
while(tries++ < MAX_RETRY && result == null) {
try {
Log.i("RssReaderTask", "********** doInBackground: Processing... Trial: " + tries);
URL url = new URL(params[0]);
RssFeed feed = RssReader.read(url);
result = feed.getRssItems();
} catch (Exception ex) {
Log.i("RssReaderTask", "********** doInBackground: Feed error!");
}
}
return result;
}
#Override
protected void onPostExecute(ArrayList<RssItem> result) {
// deal with result
}
}
I am creating an android app for my facebook page. The app is supposed to display random statuses(not just the recent ones) from the facebook page. Is there anyway I could do this?
I haven't done anything of that kind ever, but I think you can gran some logic from this and get it to work.
Step 1:
Make a call to the Facebook API, get all Status Updates and in a for loop, add them to an ArrayList<String>. For example, Facebook returns its data in JSON format. I am assuming, you already know how to fetch data. You need to parse the "message" tag from the JSON data returned by your Facebook API call.
For example:
ArrayList<String> arrStatusMessage;
for (int i = 0; i < JAFeeds.length(); i++) {
JSONObject JOFeeds = JAFeeds.getJSONObject(i);
if (JOFeeds.has("message")) {
String strStatusMessage = JOFeeds.getString("message");
arrStatusMessage.add(strStatusMessage );
}
}
Step 2:
Once you have your entire set of Facebook Status Messages, you will now need to use a java.util.Random instance.
For example: (Please note: I have not tested this code and it might result in errors. You may have to play around with it a bit to get it to work. :-( )
private static final Random randomGenerator = new Random();
int intRandom = randomGenerator.nextInt(arrStatusMessage.size());
String strRandomStatus = arrStatusMessage.get(intRandom);
Step 3:
Use the strRandomStatus to set it on a TextView.
For example:
TextView txtRanStatus = (TextView) findViewById(R.id.txtRanStatus);
txtRanStatus.setText(strRandomStatus);
You haven't posted any code, so it is difficult to provide something that fits in your scheme of things. But I think this should get you started. You will, possibly, need to adapt a few things and fit them in your own code.
Hope this helps.
EDIT: As per a comment by th OP, adding some bits of code to fetch Facebook Status Messages:
in your onCreate() method:
Start a new AsyncTask:
new getFacebookFeeds().execute();
I use this method in my app to make the Facebook Call to get all feeds from the Graph API.
private class getFacebookFeeds extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
String URL = "https://graph.facebook.com/me/home&access_token=ACCESS_TOKEN?limit=10";
try {
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(URL);
HttpResponse rp = hc.execute(get);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(rp.getEntity());
// GET THE INTIAL RESULTS JSON ROOT
JSONObject JORoot = new JSONObject(result);
// GET THE "DATA" TAG FOR FEEDS ROOT
JSONArray JAFeeds = JORoot.getJSONArray("data");
for (int i = 0; i < JAFeeds.length(); i++) {
JSONObject JOFeeds = JAFeeds.getJSONObject(i);
if (JOFeeds.has("message")) {
String strStatusMessage = JOFeeds.getString("message");
arrStatusMessage.add(strStatusMessage );
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
You can do the remaining code, where you select a random Status Update, in the onPostExecute() of the AsyncTask shown above:
#Override
protected void onPostExecute(Void result) {
int intRandom = randomGenerator.nextInt(arrStatusMessage.size());
String strRandomStatus = arrStatusMessage.get(intRandom);
txtRanStatus.setText(strRandomStatus);
}
Declare the TextView as a Global Variable and cast it on your onCreate() before calling the AsyncTask. I think this should work just fine. Let me know how it goes. :-)
I tried to refer similar question on SO, but didn't got any help.
In my android app, I'm planning to implement Recent Quote the user has visited i.e. similar to recently visited pages on web.
Following are the steps I'm following:
1.) Whenever user opens any company view, fetch the company symbols from database
2.) Then store the current symbol along with dateTime in database.
3.) For all symbols fetched from database, Fetch their current value and %Change and display Company name, current value and %Change in a list.
The problem arises in the ASyncTask class as postExecute method doesn't allow it's return type to be any other than void.
Am I doing anything wrong?
Any help will be life saver !!!
String[] rsym,rcmp,rchg;
rdbM = new RecentDBManager(CompanyView.this);
try {
Calendar date1 = Calendar.getInstance();
SimpleDateFormat dateformatter = new SimpleDateFormat(
"dd/MM/yyyy HH:mm:ss");
String currentdate = dateformatter.format(date1.getTime());
rdbM.openDB();
//STEP 1
rsym = rdbM.getRecent_sym();
//STEP 2
rdbM.setData(currentsymbol, currentdate);
rdbM.closeDB();
} catch (Exception e) {
throw new Error(" *** ERROR in DB Access *** "+ e.toString());
}
//STEP 3
for(int i=0;i<rsym.length;i++)
{
DownloadRecentQuote quotetask = new DownloadRecentQuote();
recentquotetask
.execute(new String[] { "http://abc.com/stockquote.aspx?id="
+ rsym[i] });
//CURRENT VALUE and %CHANGE which should be returned from ASyncTask class
rcmp[i]=valuearr[0];
rchg[i]=valuearr[1];
}
list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> addList1;
for (int i = 0; i < limit; i++)
{
addList1 = new HashMap<String, String>();
addList1.put(RecentSym_COLUMN, rsym[i]);
addList1.put(RecentCMP_COLUMN, rcmp[i]);
addList1.put(RecentChg_COLUMN, rchg[i]);
list1.add(addList1);
RecentAdapter adapter1 = new RecentAdapter(
CompanyView.this, CompanyView.this, list1);
listrecent.setAdapter(adapter1);
}
private class DownloadRecentQuote extends AsyncTask<String, Void, String> {
/* Fetching data for RecentQuote information */
#Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
#Override
protected void onPostExecute(String result) {
arr1 = result.split("#");
if (arr1[0].length() != 0) {
if (arr1[0].equals("1")) {
arr = arr1[1].split(";");
//RETURN 2 STRINGS
String valuearr[];
valuearr[0] = arr[3];
valuearr[1] = arr[6].concat("%");
//return valuearr;
}
}
}
postExecute() can't return a value because who or what would it return to? Your original method that invoked the AsyncTask is gone because your AsyncTask is running in the background. It's asynchronous meaning when AsyncTask.execute() returns it's still running in the background, and hence postExecute() can't return a value because there's nothing to return it to.
Instead your AsyncTask needs a reference back to your Activity or some other object so it can post your values back to it. In your code the lines after you call execute() can't be there because your task hasn't finished. Instead you should create a method called updateSymbol( currentPrice, percentChange), move all that code below execute() in there, and in your AsyncTask you should pass a reference to the Activity. Then call updateSymbol( currentPrice, percentChange ) from the onPostExecute() method.
But, be careful if you have a reference back to an Activity it can be destroyed while your doInBackground() is running, and when postExecute() runs it should just drop the results or not attempt to update the UI. For example, the user rotates their phone causing the Activity to be destroyed. I find it best to hold a reference to the AsyncTask in the activity so it can cancel() it if the Activity is destroyed. You can call AsyncTask.cancel() then check if your task was canceled like:
public void postExecute( String result ) {
if( !isCanceled() ) {
// do your updating here
activity.setSymbol( result );
}
}
It's really easy to create a base class for all Activities so you can easily keep track of AsyncTasks running:
public class BaseActivity extends Activity {
List<AsyncTask> runningTasks;
public void onStop() {
for( AsyncTask task : runningTasks ) {
task.cancel(true);
}
}
public AsyncTask start( AsyncTask task ) {
runningTasks.add( task );
return task;
}
public void done( AsyncTask task ) {
runningTasks.remove( task );
}
}
Some quick pointers. You don't need execute( new String[] { "blah" + blah } ). Varargs in Java allow you to do this. execute( "blah" + blah ). You also are catching exceptions and continuing without really handling them. It will be hard when something really happens because your app catches them, and just continues as if nothing happened. If you get an error you might want to provide some feedback to the user and stop trying to execute that process. Stop, show an error to the user, and let them do the next thing. Move the catch blocks to the bottom of the methods.
Essentially, AsyncTask.onPostExecute() is where you do whatever you want to do after AsyncTask's doInBackground() is executed and the execution result gets returned. This should be considered the best practice.
When AsyncTask().execute() is called from the UI thread (note that this method must be called from the UI thread), the Android framework creates a worker thread and starts running whatever you wrote in AsyncTask.doInBackground() on this worker thread. At this point (after calling new AsyncTask().execute()), the UI thread continues to execute code after new AsyncTask().execute(). So now during run time, you have two threads (UI and worker thread) both running simultaneously.
But where and when does the AsyncTask execution result get returned from the worker thread back to the UI thread?
The point where your worker thread (doInBackground()) finishes and returns to the UI thread is AysncTask.onPostExecute(). This method is guaranteed to be called by the framework on the UI thread as soon as AsyncTask finishes. In other words, we don't care where and when AsyncTask.onPostExecute() gets called at run time, we just need to guarantee it will be called ultimately at some stage in the future. This is the reason why this method does not return an execution result - instead, it requires that the execution result gets passed in as the only method parameter from doInBackground().
In addition, the Android API provides a way to return an AsyncTask execution result at coding time, AsyncTask.get():
MyAsyncTask myAsyncTask = new MyAsyncTask();
// This must be called from the UI thread:
myAsyncTask.execute();
// Calling this will block UI thread execution:
ExecutionResult result = myAsyncTask.get();
Bear in mind that AsyncTask.get() will block the calling thread's execution, and you will probably get an ANR exception if you call it on the UI thread. This is the payload of using AsyncTask.get(), by calling it on the UI thread, you are actually making your AsyncTask (worker thread) run synchronously with UI thread (by making UI thread wait). To sum up, this is doable but not recommended.
Just for future reference, because this post is a little old:
I have created an Activity class which has an onStart() method and a separate class for the AsyncTask. Based on my test, after the doInbackground() method the result will be sent to the activity first and after that onPostExecute() will run. This is because based off of logcat, I have my first response data (sent by server) first, then this response will show again from the activity and the last the message in onPostExecute() will show.
Code for the activity:
#Override
protected void onStart() {
super.onStart();
String str = "***";
if(isConnectedToInternet()){
myAsyncTask.execute();
try {
if(myAsyncTask.get())
str = myAsyncTask.getResponseMsg();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (CancellationException e) {
e.printStackTrace();
}
}
Log.i("Data returned by server2:", str);
}
AsyncTask code:
public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
private URL url;
private HttpURLConnection conn;
private String strResponseMsg;
public MyAsyncTask(String url) throws MalformedURLException{
this.url = new URL(url);
}
#Override
protected void onPreExecute() {
Log.i("Inside AsyncTask", "myAsyncTask is abut to start...");
}
#Override
protected Boolean doInBackground(Void... params) {
boolean status = false;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(Manager.ConnTimeout);
conn.setReadTimeout(Manager.ReadTimeout);
int responseCode = conn.getResponseCode();
Log.i("Connection oppened", "Response code is:" + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
if (in != null) {
StringBuilder strBuilder = new StringBuilder();
// Read character by character
int ch = 0;
while ((ch = in.read()) != -1)
strBuilder.append((char) ch);
// Showing returned message
strResponseMsg = strBuilder.toString();
Log.i("Data returned by server:", strResponseMsg);
status = true;
}
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
#Override
protected void onPostExecute(Boolean result) {
Log.i("Inside AsyncTask", "myAsyncTask finished its task. Returning data to caller...");
}
public String getResponseMsg(){
return strResponseMsg;
}
}
I'm using using AsyncTask to download data over internet and I have a little problem. I need to be able to start one AsyncTask a few times, that's why I'm creating a new instance everytime,but the thing that I notice is that it's working without any problem the first three or four times,but after that my AsyncTask is stuck on onPreExecute() and doing nothing after that. Am I doing something wrong ? (Actually I am using two AsyncTasks one after another just for testing purposes). Here is the sample code which I'm using :
this is how I start the AsyncTasks :
if (index == 1) {
//Login - first way
new FirstSync().execute(Synchronization.this);
} else if (index == 2) {
//SyncWithHash - second way
SyncWithHash syncHash = new SyncWithHash();
syncHash.execute(Synchronization.this);
} else if (index == 3) {
//Deactivate Collection - third way
deactivateColl = new DeactivateCollection();
deactivateColl.execute(Synchronization.this);
}
I did try with three different ways to start the asyncTask,but no change. Here is my AsyncTask :
// Sync With Hash
public class SyncWithHash extends AsyncTask <Context, Integer, Void> {
#Override
protected Void doInBackground(Context... arrContext) {
try {
String charset = "UTF-8";
hash = getAuthHash();
SharedPreferences lastUser = PreferenceManager.getDefaultSharedPreferences(Synchronization.this);
int userId = lastUser.getInt("lastUser", 1);
systemDbHelper = new SystemDatabaseHelper(Synchronization.this, null, 1);
systemDbHelper.initialize(Synchronization.this);
String sql = "SELECT dbTimestamp FROM users WHERE objectId=" + userId;
Cursor cursor = systemDbHelper.executeSQLQuery(sql);
if (cursor.getCount() < 0) {
cursor.close();
} else if (cursor.getCount() > 0) {
cursor.moveToFirst();
timeStamp = cursor.getString(cursor.getColumnIndex("dbTimestamp"));
Log.d("", "timeStamp : " + timeStamp);
}
String query = String.format("debug_data=%s&"
+ "client_auth_hash=%s&" + "timestamp=%s&"
+ "client_api_ver=%s&"
+ "set_locale=%s&" + "device_os_type=%s&"
+ "device_sync_type=%s&"
+ "device_identification_string=%s&"
+ "device_identificator=%s&" + "device_resolution=%s",
URLEncoder.encode("1", charset),
URLEncoder.encode(hash, charset),
URLEncoder.encode(timeStamp, charset),
URLEncoder.encode(clientApiVersion, charset),
URLEncoder.encode(locale, charset),
URLEncoder.encode(version, charset),
URLEncoder.encode("14", charset),
URLEncoder.encode(version, charset),
URLEncoder.encode(deviceId, charset),
URLEncoder.encode(resolution, charset));
SharedPreferences useSSLConnection = PreferenceManager
.getDefaultSharedPreferences(Synchronization.this);
boolean useSSl = useSSLConnection.getBoolean("UseSSl", true);
if (useSSl) {
UseHttpsConnection(url, charset, query);
} else {
UseHttpConnection(url, charset, query);
}
} catch (Exception e2) {
e2.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Integer... progress) {
//cancelDialog.setProgress(progress[0]);
}
#Override
protected void onCancelled() {
Log.d("","ON CANCELLED");
}
#Override
protected void onPreExecute()
{
Log.d("","ON PRE EXECUTE");
// myProgress = 0;
}
#Override
protected void onPostExecute(Void v) {
Log.d("","ON POST EXECUTE");
}
}
So any ideas why it's happening and which is the best way to be able to use an AsyncTask a few times without any exceptions and bugs like the one that I get.
And another question : Is there anything in AsyncTask which can cause my connection to be Reset by peer , because I'm getting this error too (not every time).
Thanks a lot!
I think your doInBackground() is hanging. Make log statement when its entered and when its exited and check.
In the old days AsyncTask had a pool of threads, so if a doInBackground() hung, then it didnt affect the other AsyncTasks. That changed AFAIK with Android 2.2 or 2.3 to that a single thread took care of all AyncTasks, one at a time. Therefore, if your doInBackground() is hanging it might affect the next AsyncTasks being started and the will hang right after onPreExecute().
Edit: It was changed from a single thread, to multiple, and then back to a single thread:
http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29 "When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. After HONEYCOMB, it is planned to change this back to a single thread to avoid common application errors caused by parallel execution."
If you really want an indefinite lot of stuff to 'hang' in parralel, then don't use AsyncTask. Use good old threads, which, when they need to update the GUI, fire off a Runnable to be runned on the GUI thread:
Button knap1, knap2, knap3;
...
Runnable r=new Runnable() {
public void run() {
// Do some stuff than hangs
try { Thread.sleep(10000); } catch (InterruptedException ex) {}
System.out.println("færdig!");
// Update GUI thread
Runnable r2=new Runnable() {
public void run() {
knap3.setText("færdig!");
}
};
runOnUiThread(r2);
}
};
new Thread(r).start();
(example from http://code.google.com/p/android-eksempler/source/browse/trunk/AndroidElementer/src/eks/asynkron/Asynkron1Thread.java?spec=svn109&r=109)
It might be happening because you are synchronizing on an object "Synchronization.this'.
Also noticed you are not closing the cursor which you opened.