Issue when setting a ListView in a AsyncTask class - android

I'd like to set a ListView to data I get from a web service. I get the data in a AsyncTask instance, but when I try to set some of my ListView attributes, it crashes (on line "lv.setVisibility(View.VISIBLE);"). Anybody can help?
thanks
public class Atable extends Activity {
private EditText mSearch;
private static final int ACTIVITY_EDIT=0;
private Button mSearchButton;
private TextView mNoresults;
private ListView lv;
private CheckBox checkBox;
private LocationManager locationManager;
private RestaurantList restaurantList;
private Criteria criteria;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv= (ListView)findViewById(R.id.listview);
mNoresults = (TextView) findViewById(R.id.noresults);
mNoresults.setVisibility(View.GONE);
mSearchButton = (Button)this.findViewById(R.id.button);
checkBox = (CheckBox) findViewById(R.id.local_check);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
mSearchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mNoresults.setVisibility(View.GONE);
mSearch = (EditText) findViewById(R.id.search);
String tmp_str = mSearch.getText().toString().replace(" ","+");
String url = "http://www.atable.org/getRestaurantByQuery/?query=" + tmp_str;
if (checkBox.isChecked()) {
//get location
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location!=null) {
String lat = String.valueOf(location.getLatitude());
String lng = String.valueOf(location.getLongitude());
url += "&lat="+lat+"&lng="+lng;
}
}
new GetRestaurantData().execute(url);
}
});
};
private class GetRestaurantData extends AsyncTask<String, Boolean, RestaurantList> {
private HttpClient httpclient = new DefaultHttpClient();
#Override
protected RestaurantList doInBackground(String... url) {
publishProgress(true);
HttpGet httpget = new HttpGet(url[0]);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Reader r = new InputStreamReader(instream);
Gson gson = new Gson();
restaurantList = gson.fromJson(r, RestaurantList.class);
int nResults = restaurantList.getSize();
if (nResults>0) {
lv.setVisibility(View.VISIBLE); //app crashes here
lv.setAdapter( new ArrayAdapter<String>(Atable.this ,android.R.layout.simple_list_item_1,restaurantList.getRestaurantNames()));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Atable.this, RestaurantDescription.class);
Restaurant tmp_resto = restaurantList.getRestaurant((int)id);
String tmp_categories = tmp_resto.getCategories().get(0);
for (int i=1; i<tmp_resto.getCategories().size(); i++) {
tmp_categories+=", "+tmp_resto.getCategories().get(i);
}
String address = tmp_resto.getStreet()+", "+tmp_resto.getStreetNumber()+"\n"+tmp_resto.getCity()+
" "+tmp_resto.getPostalCode()+"\n"+tmp_resto.getCountry();
intent.putExtra("name", tmp_resto.getName());
intent.putExtra("address", address);
intent.putExtra("rating", tmp_resto.getRating());
intent.putExtra("price_range", tmp_resto.getPriceRange());
intent.putExtra("categories", tmp_categories);
intent.putExtra("latitude", tmp_resto.getLatitude());
intent.putExtra("longitude", tmp_resto.getLongitude());
startActivityForResult(intent, ACTIVITY_EDIT);
}
});
}
else {
lv.setVisibility(View.GONE);
mNoresults.setVisibility(View.VISIBLE);
}
//Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return restaurantList;
}
#Override
protected void onProgressUpdate(Boolean... progress) {
// line below coupled with
// getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS)
// before setContentView
// will show the wait animation on the top-right corner
Atable.this.setProgressBarIndeterminateVisibility(progress[0]);
}
#Override
protected void onPostExecute(RestaurantList result) {
publishProgress(false);
// Do something with result in your activity
}
}

In Android, all UI updates are done on the main thread (also called UI thread). It's good when you spawn a new thread to do time consuming tasks in order not to block the UI.
But in order to avoid all sorts of indeterminate behaviors, UI updates always have to be done on the UI thread. If you ever attempt to do so from a different thread, an exception will be thrown.
In your example, I see you're performing different updatings to ListView based on the value of nResults. You can try returning nResults for doInBackground(). It will be passed to onPostExecute(), which will be executed on the UI thread.
You wanna check out this article for some useful information about threadings in Android: http://android-developers.blogspot.com/2009/05/painless-threading.html
HTH

You can't access the ui thread from the doinbachground method.. you need to do that from the post execute, pre execute or on progress update methods...

Related

Loading listview Activity takes long and show black screen before appear

I created app that takes JSON with AsyncTask from server. When User click a button app starts new Activity and download data from server and show it as a items in ListView. The Problem is when I open new Activity it takes too long to load. When button is pressed app freeze on about one or two seconds and then show black screen for another 2/3 seconds. After that activity is displayed but it is very slow. It freeze every time user is scrolling or pressing button to display more options of custom adapter. Is there any way to make app more smooth? Json data that is downloaded is just simple JSONArray with JSONObjects that has 2 string values and one HTML format. This 3 values is display to user.
Part of Custom Adapter class
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
SuggestionList suggestionList = getItem(position);
int actualPosition = 0;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.sugestion_list, parent, false);
}
final Button suggestionsButton = (Button) convertView.findViewById(R.id.suggestionsMore);
final TextView suggestionNumber = (TextView) convertView.findViewById(R.id.sugestionNumber);
final TextView suggestionDescription = (TextView) convertView.findViewById(R.id.suggestionDescription);
final ImageView bio = convertView.findViewById(R.id.sugestionBio);
final ImageView block = convertView.findViewById(R.id.sugestionBlock);
final ImageView call = convertView.findViewById(R.id.sugestionCall);
...
final Animation slideUp = AnimationUtils.loadAnimation(getContext(), R.anim.slideup);
final Animation slideDown = AnimationUtils.loadAnimation(getContext(), R.anim.slidedown);
final Handler handler = new Handler();
suggestionsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (bioSuggestions.getVisibility() == View.GONE) {
bio.setVisibility(View.VISIBLE);
block.setVisibility(View.VISIBLE);
call.setVisibility(View.VISIBLE);
bioSuggestions.startAnimation(slideUp);
blockSuggestions.startAnimation(slideUp);
callSuggestions.startAnimation(slideUp);
} else if (bioSuggestions.getVisibility() == View.VISIBLE) {
bioSuggestions.startAnimation(slideDown);
blockSuggestions.startAnimation(slideDown);
callSuggestions.startAnimation(slideDown);
handler.postDelayed(new Runnable() {
#Override
public void run() {
bio.setVisibility(View.GONE);
block.setVisibility(View.GONE);
call.setVisibility(View.GONE);
}
}, 300);
}
}
});
if (actualPosition != position) {
if (bio.getVisibility() == View.VISIBLE) {
bio.setVisibility(View.GONE);
block.setVisibility(View.GONE);
call.setVisibility(View.GONE);
}
actualPosition = position;
}
JSONObject jsonValSuggestions = new getSugestions().sugestionsDetails(position, "suggestions");
try {
final String name = jsonValSuggestions.getString("client_name");
final String num = jsonValSuggestions.getString("client_number");
final String description = jsonValSuggestions.getString("client_description");
bio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent suggestionsDetails = new Intent(view.getContext(), SuggestionsDetails.class);
suggestionsDetails.putExtra("client_number", num);
suggestionsDetails.putExtra("client_name", name);
suggestionsDetails.putExtra("client_description", description);
activity.startActivityForResult(suggestionsDetails, position);
}
});
block.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent suggestionBlock = new Intent(view.getContext(), BlockSuggestionsActivity.class);
activity.startActivity(suggestionBlock);
}
});
call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent suggestionCall = new Intent(view.getContext(), CallSuggestionActivity.class);
suggestionCall.putExtra("client_number", num);
suggestionCall.putExtra("client_name", name);
activity.startActivity(suggestionCall);
}
});
} catch (Exception e) {
e.printStackTrace();
}
try {
if (suggestionList.suggestionName.equals("null") || suggestionList.suggestionName.equals("")) {
suggestionNumber.setText(suggestionList.suggestionNumber);
} else {
suggestionNumber.setText(suggestionList.suggestionName);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
suggestionDescription.setText(Html.fromHtml(suggestionList.suggestionDescription, Html.FROM_HTML_MODE_LEGACY));
} else {
suggestionDescription.setText(Html.fromHtml(suggestionList.suggestionDescription));
}
} catch (Exception e) {
Log.i("exception", e.getMessage());
}
return convertView;
}
Part of AsyncTask class
public static final String REQUEST_METHOD = "GET";
public static final int READ_TIMEOUT = 15000;
public static final int CONNECTION_TIMEOUT = 15000;
#Override
protected String doInBackground(String... params) {
String clientUrl = params[0];
String result;
String inputLine;
JSONObject obj;
String data;
String message;
try {
URL myUrl = new URL(clientUrl);
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.connect();
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
while ((inputLine = reader.readLine()) != null) {
stringBuilder.append(inputLine);
}
reader.close();
streamReader.close();
result = stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
result = null;
}
return result;
}
public String[] getSuggestionsList() {
String[] suggestionList = new String[5];
String result;
String status;
JSONObject listObj;
String suggestionsData;
JSONObject suggestionsDataObj;
JSONArray suggestionsDataArr;
String ClientsSugestionsUrl = "https://example.com/token=" + authToken;
getApiClientSugestions getSugestionsFromApi = new getApiClientSugestions();
try {
result = getSugestionsFromApi.execute(ClientsSugestionsUrl).get();
try {
listObj = new JSONObject(result);
status = listObj.getString("result");
suggestionsData = listObj.getString("suggestions");
suggestionsDataArr = new JSONArray(suggestionsData);
} catch (Exception e) {
e.printStackTrace();
suggestionsDataArr = null;
status = null;
}
suggestionList[3] = status;
suggestionList[4] = suggestionsDataArr.toString();
} catch (Exception e) {
e.printStackTrace();
}
return suggestionList;
}
Activity
public class CallsSuggestionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calls_suggestions);
Slidr.attach(this);
getSupportActionBar().setTitle("Skontaktuj się");
}
#Override
protected void onResume() {
super.onResume();
CallsSuggestionList();
}
public void CallsSuggestionList() {
final ListView suggestionList = findViewById(R.id.sugestionList);
final ArrayList<SuggestionList> suggestionArray = new ArrayList<SuggestionList>();
SuggestionListAdapter suggestionListAdapter = new SuggestionListAdapter(getContext(), suggestionArray, this);
String[] suggestionListArray = new getSugestions().getSuggestionsList();
String suggStat = suggestionListArray[3];
String arrayList = suggestionListArray[4];
String clientName;
String clientNumber;
String clientDescription;
try {
JSONArray jsonArray = new JSONArray(arrayList);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
clientName = explrObject.getString("client_name");
clientNumber = explrObject.getString("client_number");
clientDescription = explrObject.getString("client_description");
if (suggStat.equals("true")) {
SuggestionList suggestionList1 = new SuggestionList(clientName, clientDescription, clientNumber);
suggestionListAdapter.addAll(suggestionList1);
suggestionListAdapter.notifyDataSetChanged();
suggestionList.setAdapter(suggestionListAdapter);
}
}
} catch (Exception e) {
Log.i("exception", e.getMessage());
e.printStackTrace();
clientName = null;
clientDescription = null;
clientNumber = null;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
SuggestionList
public class SuggestionList {
public String suggestionNumber;
public String suggestionDescription;
public String suggestionCallType;
public String suggestionName;
public SuggestionList(
// String suggestionCallType,
String suggestionName, String suggestionDescription, String suggestionNumber) {
this.suggestionNumber = suggestionNumber;
// this.suggestionCallType = suggestionCallType;
this.suggestionName = suggestionName;
this.suggestionDescription = suggestionDescription;
}
}
Adapter are custom with custom view displayed to user. I use similar custom adapter to show content from sqlite that is on phone and there app isn't so slow. But when I open this activity it slow down dramatically. Also I noticed when I press back button it take very long to back to previous screen.
The problem is in the getSuggestionsList function. in this function, you are calling getSugestionsFromApi.execute(ClientsSugestionsUrl).get(); which make your code sync again. I mean your code is waiting this code to be executed.
One way (not right way, but easy way): you can call new getSugestions().getSuggestionsList(); in a new thread.
Second way, call getSugestionsFromApi.execute(ClientsSugestionsUrl) without get() function. But to get result of the code, you need to give an interface.
To get right usage: https://xelsoft.wordpress.com/2014/11/28/asynctask-implementation-using-callback-interface/

ResourcesNotFoundException: String resource ID #0x0 (No Response: UI Thread)

I am using OkHttpClient to get the Staff List (in JSON form) as response using AsyncTask Function. After getting the response in AsyncTask funtion, while inserting staff list in database, I want to show the count on the screen. but it gives following error. I tried many solutions, but couldn't resolve.
Following is the synchronizeStaffList Activity that calls SenkronizeEt Funtion using handler.
public class synchronizeStaffList extends AppCompatActivity {
DatabaseHelper gksDatabase;
TextView txtrecordValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_synchronize_staff_list);
gksDatabase = new DatabaseHelper(this);
txtrecordValue = (TextView) findViewById(R.id.txtrecordValue);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
SenkronizeEt();
}
}, 500);
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(synchronizeStaffList.this);
builder.setCancelable(false);
if (progressStaffListInfo == 0) {
builder.setMessage("Sunucu ile iletişim yok");
} else if (progressStaffListInfo == 1) {
builder.setMessage("Sicil/Personel listesi güncellendi");
}
builder.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// if user pressed "MINIMIZE", cancel this dialog & minimize the application
// while attendance process will keep working in the back
// finish();
startActivity(new Intent(synchronizeStaffList.this, synchronize.class));
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}, 5000);
}
I uses second handler to show an alertbox.
Following the SenkronizeEt Function that call OkHttpHandler AsyncTask function to get the okHttpResponse/result
public int progressStaffListInfo = 0; // 0: List no updated, 1: List updated,
private void SenkronizeEt() {
progressStaffListInfo = 0;
// Updating Staff List in device by calling webservices
HashMap<String, String> Parametre = gksDatabase.GetParameter();
final String urlStaffList = "http://127.0.0.1:8080/JAXRSJsonCRUDExample/rest/employees/" + Parametre.get("DeviceNo");
OkHttpHandler handler = new OkHttpHandler();
String jsonEmployeesList = null;
try {
jsonEmployeesList = handler.execute(urlStaffList).get();
} catch (Exception e) {
//tv.setText("sorry, something went wrong !");
}
if (progressStaffListInfo == 0) {
Toast.makeText(synchronizeStaffList.this, "List Not Updated", Toast.LENGTH_SHORT).show();
} else if (progressStaffListInfo == 1) {
Toast.makeText(synchronizeStaffList.this, "List updated in database", Toast.LENGTH_SHORT).show();
}
}
Here is the OkHttpHandler AsyncTask function that call the URL to get the response and then update it in database.
public class OkHttpHandler extends AsyncTask<String, Integer, String> {
OkHttpClient client = new OkHttpClient();
#Override
protected String doInBackground(String... params) {
Request.Builder builder = new Request.Builder();
builder.url(params[0]);
Request request = builder.build();
try {
Response response = client.newCall(request).execute();
// return response.body().string();
String jsonData = response.body().string();
//JSONObject Jobject = new JSONObject(jsonData);
//JSONArray Jarray = Jobject.getJSONArray("employees");
JSONArray Jarray = new JSONArray(jsonData);
//get the length of the json array
int limit = Jarray.length();
if (limit != 0) {
gksDatabase.PersonsDelete();
progressStaffListInfo = 1; // Staff List updated
}
for (int i = 0; i < limit; i++) {
JSONObject object = Jarray.getJSONObject(i);
//store the data into database
// SId:StaffID SNo:StaffNumber SN:StaffName CNo:CardNumber
gksDatabase.PersonsSave(Integer.parseInt(object.getString("sid")), object.getString("sno"), object.getString("sn"),
object.getString("cno")
);
publishProgress((int) (((i+1) / (float) limit) * 100));
}
return jsonData;
} catch (JSONException e) {
progressStaffListInfo = 0;
} catch (Exception e) {
e.printStackTrace();
progressStaffListInfo = 0;
}
return null;
}
#Override
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
}
private void setProgressPercent(int progValue){
txtrecordValue.setText(progValue);
}
In above AsyncTask function, it gives error when it try to display the count of entries inserted in database.
Actually, not only this. if I set any thing (Like progressbar or count) on different thread, even then it stops till the asyncTask finishes its work. As an example AlertDialog handler also starts when AsyncTask function finishes all its work.
Following are the logs:
E/AndroidRuntime: FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:249)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3796)
at com.emiturk.gsk.synchronizeStaffList.setProgressPercent(synchronizeStaffList.java:172)
at com.emiturk.gsk.synchronizeStaffList.access$100(synchronizeStaffList.java:25)
at com.emiturk.gsk.synchronizeStaffList$OkHttpHandler.onProgressUpdate(synchronizeStaffList.java:167)
at com.emiturk.gsk.synchronizeStaffList$OkHttpHandler.onProgressUpdate(synchronizeStaffList.java:116)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4810)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method
Kindly help me out.
You cannot set int value on textView so it should be a String object so do it like
private void setProgressPercent(int progValue){
txtrecordValue.setText(""+progValue);
}
""+ will implicitly convert your progValue to String as if first parameter is a string (which it is empty string "") along with + then other will be promoted to string

AsyncTask not running onPostExexute in Activity launched from Notification

I am writing an Android App that checks for new messages in the background using AlarmManager and WakefulBroadcastReceiver. The Receiver starts an IntentService and that uses an AsyncTask for the network request (HTTPClient).
After a reboot, the alarm is re-enabled using a BroadcastReceiver listening for android.intent.action.BOOT_COMPLETED.
The problem occures when the onCreate method of my main Activity loads the full message contents using an AsyncTask, but only under certain circumstances:
java.lang.RuntimeException: Handler (android.os.AsyncTask$InternalHandler) {4139f618} sending message to a Handler on a dead thread
The doInBackground is executed and returns a String with valid content, but instead of executing onPostExecute, the excaption rises.
There are several scenarios:
I start the app from the launcher -> WORKS.
The App has been started like (1), sent to background and then a message arrives. The Notification is tapped, the Activity starts -> WORKS.
The App has been started like (1), send to background and the device was rebooted. A message arrives, the notification is tapped, the Activity starts -> EXCEPTION.
Question: How can I fix that, without burning in the developer hell for doing network requests on the UI thread?
Searching for a solution, I found onPostExecute not being called in AsyncTask (Handler runtime exception), but neither the solution from "sdw" nor from "Jonathan Perlow" changes a thing.
ServerQuery:
public abstract class ServerQuery extends AsyncTask<Void, Void, String> {
protected Context context;
String user = "ERR";
String pin = "ERR";
String cmd = "ERR";
ServerQuery(Context context, String _user, String _pin, String _cmd) {
this.context = context;
user = _user;
pin = _pin;
cmd = _cmd;
}
private ServerQuery() {
}
#Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(context.getString(R.string.post_client), context.getString(R.string.post_client_app)));
nameValuePairs.add(new BasicNameValuePair(context.getString(R.string.post_user), user));
nameValuePairs.add(new BasicNameValuePair(context.getString(R.string.post_pin), pin));
nameValuePairs.add(new BasicNameValuePair(context.getString(R.string.post_cmd), cmd));
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://[my server hostname]/index.php");
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
return "ERR";
}
HttpResponse httpResponse = null;
try{
httpResponse = httpClient.execute(httpPost);
}catch (Exception e) {
return "ERR";
}
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = null;
try {
inputStream = httpEntity.getContent();
} catch (IOException e) {
return "ERR";
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String s = null;
try {
while ((s = bufferedReader.readLine()) != null) {
stringBuilder.append(s);
}
} catch (IOException e) {
return "ERR";
}
try {
inputStream.close();
} catch (IOException e) {
return "ERR";
}
return stringBuilder.toString();
}
}
lastID:
public class lastID extends ServerQuery {
Integer lastid, savedid;
lastID(Context context, String _user, String _pin, String _cmd) {
super(context, _user, _pin, _cmd);
lastid = -1;
}
#Override
protected void onPostExecute(final String success) {
if(success.equals("ERR") || success.length() < 1) {
Toast.makeText(context, context.getString(R.string.server_no_connection), Toast.LENGTH_LONG).show();
} else {
String content[] = success.split("(;)");
if(content.length == 2) {
lastid = Integer.parseInt(content[0]);
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getString(R.string.settings_filename), 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
savedid = sharedPreferences.getInt(context.getString(R.string.post_lastid), -1);
if (lastid > savedid) { // NEUES ANGEBOT!
editor.putInt(context.getString(R.string.post_lastid), lastid);
editor.commit();
// Benachrichtung
NotificationCompat.Builder notific = new NotificationCompat.Builder(context);
notific.setContentTitle(context.getString(R.string.notify_title));
notific.setContentText(content[1]);
notific.setSmallIcon(R.drawable.ic_notify);
notific.setAutoCancel(false);
notific.setPriority(NotificationCompat.PRIORITY_HIGH);
notific.setTicker(content[1]);
notific.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
notific.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(lastid, notific.build());
}
}
}
}
}
MainActivity onCreate:
[ loading shared preferences, setting onclicklisteners ]
angebot = new Angebot(this, getApplicationContext(), user, pin, getString(R.string.post_cmd_viewAngebot));
angebot.execute((Void) null);
Angebot is like lastID, but it uses the String to fill the TextViews.
EDIT 1:
Angebot:
public class Angebot extends ServerQuery {
protected Activity activity;
protected TextView datumView;
// more TextView s
protected Button hungerButton;
Angebot(Activity activity, Context context, String _user, String _pin, String _cmd) {
super(context, _user, _pin, _cmd);
this.activity = activity;
datumView = (TextView) this.activity.findViewById(R.id.datum);
// finding all other TextView s
}
#Override
protected void onPreExecute() {
showProgress(true);
}
#Override
protected void onPostExecute(final String success) {
Log.v(C.TAG_ALARMESSEN, "Angebot.onPostExecute");
showProgress(false);
if(success.equals("ERR") || success.length() < 1) {
Toast.makeText(activity.getApplicationContext(), context.getString(R.string.server_no_connection), Toast.LENGTH_LONG).show();
} else {
String content[] = success.split("(;)");
// put each content string in a TextView
} else {
Toast.makeText(context, context.getString(R.string.err02s), Toast.LENGTH_LONG).show(); // nicht angemeldet
}
}
}
#Override
protected void onCancelled() {
showProgress(false);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void showProgress(final boolean show) {
final View progressView = activity.findViewById(R.id.login_progress);
progressView.setVisibility(show ? View.VISIBLE : View.GONE);
}
}
EDIT 2: stack trace:
java.lang.RuntimeException: Handler (android.os.AsyncTask$InternalHandler) {4139f618} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
at android.os.Handler.sendMessageAtTime(Handler.java:473)
at android.os.Handler.sendMessageDelayed(Handler.java:446)
at android.os.Handler.sendMessage(Handler.java:383)
at android.os.Message.sendToTarget(Message.java:363)
at android.os.AsyncTask.postResult(AsyncTask.java:300)
at android.os.AsyncTask.access$400(AsyncTask.java:156)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
I would assume that the workarounds for the AsyncTask bug mentioned here: onPostExecute not being called in AsyncTask (Handler runtime exception) would work in this case too but according to the OP they don't.
Instead of fixing the issue I would suggest to use an AsyncTaskLoader instead. There's plenty of articles about the difference (mainly the advantages) of AsyncTaskLoader compared to AsyncTask. Here are just two (good) examples):
http://www.javacodegeeks.com/2013/01/android-loaders-versus-asynctask.html and http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html
The main advantages of an AsyncTaskLoader is that they are synced with the life cycle of the Activity or Fragment that started it and that they can deal with configuration changes gracefully (unlike AsyncTasks).
Here's how you'd implement it. First you need the AsyncLoaderClass:
class ServerQuery extends AsyncTaskLoader<String> {
String mResult;
public ServerQuery(Context context) {
super(context);
}
#Override
protected void onStartLoading() {
if (mResult == null) {
forceLoad(); // no data yet -> force load it
}
else {
deliverResult(mResult); // already got the data -> deliver it
}
}
#Override
public String loadInBackground() {
// load your data...
return mResult; // return the loaded data
}
#Override
public void cancelLoadInBackground() {
// do whatever it takes to stop loading
}
}
Replace the "// load your data..." part with whatever you have in your doInBackground method. That's all there is to the actual AsyncTaskLoader. As you can see there's no equivalent to onPostExecute and that's the beauty of the AsyncTaskLoader, it's there to load data, nothing else. All the issues that occurred because AsyncTask tried to update the ui on the onPostExecute are gone.
The logic in onPostExecute now resides completely in the Activity/Fragment that starts the loader. Here's how you do it:
LoaderManager loaderMgr = getLoaderManager();
loaderMgr.initLoader(0, null, this);
this stands for LoaderManager.LoaderCallbacks meaning you have to implement that interface in your fragment/activity like so:
#Override
public Loader<String> onCreateLoader(int id, Bundle args) {
return new ServerQuery(this);
}
#Override public void onLoaderReset(Loader<String> loader) {}
#Override
public void onLoadFinished(Loader<String> loader, String data) {
// here goes your code to update the ui (previously onPostExecute);
}
onCreateLoader simply creates your loader (ServerQuery in this case) which will be managed by the LoaderManager henceforth. onLoadFinished will be called when the data is delivered to your fragment/activity allowing you to update the ui. You can check out one of my other answers to get some more information about Loaders that might help you understand how they work: https://stackoverflow.com/a/20916507/534471. Especially the part about configuration changes might be helpful.

Caching text and images using Volley library

One of the feature of the Volley library for network operations in android is that it caches text and images into disk so by changing orientation the application does not have to download images and texts again. this piece of code uses this feature:
public class AppsActivity extends Activity {
static public Typeface font;
List<Application> appsList;
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> description = new ArrayList<String>();
AppAdapter adapter;
GridView gv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apps);
font =Typeface.createFromAsset(getAssets(), "fonts/BMitra.ttf");
appsList = new ArrayList<Application>();
gv = (GridView) findViewById(R.id.gridView1);
adapter = new AppAdapter(this, appsList);
gv.setAdapter(adapter);
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get("http://appline.ir/index.php/android.feed?limitstart=");
if (entry != null)
{
fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
parseXmlString(data);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
else
{
StringRequest request = new StringRequest(Method.GET, "http://appline.ir/index.php/android.feed?limitstart=", new Listener<String>() {
#Override
public void onResponse(String res)
{
parseXmlString(res);
}
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent intent = new Intent(AppsActivity.this,
AppDetailActivity.class);
Application app;
app=appsList.get(position);
intent.putExtra("title", app.getTitle());
intent.putExtra("imageUrl", app.getImageUrl());
intent.putExtra("description", description.get(position+1));
// intent.putExtra("title", app.getTitle());
startActivity(intent);
}
});
}
public void parseXmlString(String string)
{
XmlPullParserFactory parser;
XmlPullParser xml = null;
try
{
parser =XmlPullParserFactory.newInstance();
xml = parser.newPullParser();
StringReader reader = new StringReader(string);
xml.setInput(reader);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
int event;
String text = null;
try {
event = xml.getEventType();
while (event != XmlPullParser.END_DOCUMENT)
{
String name=xml.getName();
switch (event)
{
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = xml.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("title")){
title.add(text);
}
else if(name.equals("link")){
// link = text;
}
else if(name.equals("description")){
description.add(text);
}
else{
}
break;
}
event = xml.next();
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
System.out.println(description.size());
for(int i = 1; i < description.size(); i++)
{
Document doc = Jsoup.parse((description.get(i)));
Elements links = doc.select("p"); // a with href
Elements pngs = links.select("img[src$=.png]");
String image = pngs.attr("src");
appsList.add(new Application(Html.fromHtml(title.get(i)).toString(), image, font));
}
adapter.notifyDataSetChanged();
}
My question is that, when I debug the code(using System.out in if clause for cache), I figured out that we never get a hit on cache. what is wrong?
the server dose not allow you to cache those files, in order to confirm my answer you can do one of these things:
download this plug in (RESTClient) for mozilla and send your request and check the header file for cache control
set break point in headerValue = headers.get("Cache-Control"); at HttpHeaderParse class and see whats going on.
all of those solutions only valid if you set your requests to be cashable shouldCache();
and just in order to accept my answer faster, look at cache control :-)

Only the original thread that created a view hierarchy can touch its views android

I'm having a this error Only the original thread that created a view hierarchy can touch its views.
But i don't know how to handle the ui in multithread, like list view and i don't know where to put it.
// Download JSON in Background
public class DownloadJSONFileAsync extends AsyncTask<String, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
}
#Override
protected Void doInBackground(String... params) {
deviceId = generateDeviceId();
elementsList.add(new BasicNameValuePair("DeviceID", deviceId));
JSONObject json = jsonParser.makeHttpRequest(
url_get_reports, "POST", elementsList);
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS_REPORT);
// successfully created
// getting JSON string from URL
try {
if (success == 1) {
reports = json.getJSONArray(TAG_Report);
report_data = new Report[reports.length()];
// looping through All Products
for (int i = reports.length()-1; i >= 0; i--) {
JSONObject c = reports.getJSONObject(i);
reportID = c.getString("reportID");
Drawable drawable = LoadImageFromWeb(url+c.getString(TAG_IMAGE));
String state = "";
if(c.getString("state").equals("1")){
state = "Pinding";
}else if(c.getString("state").equals("2")){
state = "Inprogress";
}else{
state = "Completed";
}
if(c.getString(TAG_TITLE).equals("")){
report_data[i] = new Report(reportID,"Report "+(i+1),c.getString(TAG_TIME),drawable,url+c.getString(TAG_IMAGE),state,c.getString("coordination")) ;
}else{
report_data[i] = new Report(reportID,c.getString(TAG_TITLE),c.getString(TAG_TIME),drawable,url+c.getString(TAG_IMAGE),state,c.getString("coordination"));
}
if(json.getInt("successReplies")==1){
replies = json.getJSONArray(TAG_REPLIES);
ArrayList<String> arr = new ArrayList<String>();
for(int j=0;j<replies.length();j++){
JSONObject c2 = replies.getJSONObject(j);
if(reportID.equals(c2.getString("ReportID")))
{
arr.add(c2.getString("RepliesContent")+"\n"+c2.getString("RepliesTime"));
}
}
report_data[i].setRepliesArray(arr);
}
}
list = (ListView)findViewById(R.id.list);
ReportAdapter adapter = new ReportAdapter(Display_Reports.this,report_data);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(
AdapterView<?> arg0, View arg1,
int position, long id) {
Intent intent = new Intent(Display_Reports.this, Display_Report_Details.class);
intent.putExtra("ID", report_data[position].ID);
intent.putExtra("Name", report_data[position].Name);
intent.putExtra("State", report_data[position].State);
intent.putExtra("Time", report_data[position].Time);
intent.putExtra("ImageUrl", report_data[position].ImageUrl);
intent.putExtra("Coordination", report_data[position].Coordination);
intent.putStringArrayListExtra("RepliesContent", report_data[position].getRepliesArray());
startActivity(intent);
}
});
} else {
// no Entities found
Toast.makeText(Display_Reports.this, "No reports found",
Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void unused) {
dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
removeDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
}
}
You cannot modify the UI from a background thread. Whether you use AsyncTask, or a Handler, or runOnUiThread(), or post(), to have work done on the main application thread is up to you.
You may wish to review the documentation on threads and some of these techniques.

Categories

Resources