Issue
I'm developing an application for android, my problem is the implementation of AsyncTask, what i must do is "simple":
This part of function is used to get information from a database, after data loading, the informations are showed in a table, I would implement a function that allows me to show a progress bar during the retrieval of information (read server)
try {
String url_img = null;
String path = "MY FANTASTIC PATH";
/*READSERVER RETURN JSON STRING THAT CONTAIN SOME IMFORMATION*/
ReadServer read = new ReadServer();
String result = read.readserver("list_news","homepage");
TableLayout MainTable = (TableLayout)findViewById(R.id.main_table);
JSONArray Jobj = new JSONArray(result);
for (int i = 0; i < Jobj.length(); i++){
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setPadding(0, 14, 2, 14);
JSONObject news_preview = Jobj.getJSONObject(i);
Integer news_id = news_preview.getInt("id_articolo");
String news_title = news_preview.getString("titolo");
String news_image = news_preview.getString("immagine");
//Check if image url is relative or absolute
Pattern p = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
Matcher m = p.matcher(news_image);
if(m.matches() == false){
url_img = path+news_image;
}else if(m.matches() == true){
url_img = news_image;
}
//Call Html Parser to parse text
HtmlParser parsed_string = new HtmlParser();
Spanned title_nohtml = parsed_string.htmlparser(news_title);
//Thumb
ImageView img = new ImageView(getApplicationContext());
img.setAdjustViewBounds(true);
img.setMaxHeight(140); img.setMaxWidth(140);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url_img).getContent());
img.setImageBitmap(bitmap);
LayoutParams params = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//Clickable title
final TextView txt = new TextView(getApplicationContext());
txt.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL );
txt.setLayoutParams(params); txt.setTextSize(18); txt.setBackgroundColor(Color.WHITE);
txt.setTypeface(null, Typeface.BOLD); txt.setTextColor(Color.BLACK);
txt.setId(news_id); txt.setText(title_nohtml); txt.setClickable(true);
row.addView(img);
row.addView(txt);
MainTable.addView(row);
txt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, OtherPages.class);
Bundle extras = new Bundle();
extras.putString("Boolean","1");
extras.putInt("id_news", txt.getId());
intent.putExtras(extras);
startActivity(intent);
}
});
}
}catch(Exception e){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Si è verificato un errore");
alertDialog.setMessage("Errore 001" +"\n"+"Non è stato possibile soddisfare la tua richiesta, riprova più tardi.");
alertDialog.show();
}
}else{
connectivityMessage("Nessuna connessione presente, assicurati di avere il traffico dati oppure il Wi-Fi attivato e riprova.");
}
I know that i must use this code but i dont know how implements, can anyone help me ?
GetNews getNewsTask = new GetNews().execute();
private class GetNews extends AsyncTask<Void, Void, Void>{
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
// do something
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(
MainActivity.this, null, "Caricamento notizie...");
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
FINAL SOLUTION:
private class GetNews extends AsyncTask<Void, Void, String>{
private ProgressDialog progress = null;
@Override
protected String doInBackground(Void... params) {
Looper.prepare(); //MUST BE ADDED
ReadServer read = new ReadServer();
String result = read.readserver("list_news","homepage");
System.out.println("DEBUG"+result);
return result;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(
MainActivity.this, null, "Caricamento notizie");
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
String url_img = null;
String path = "http://www.MYFANTASTICPATH.it";
TableLayout MainTable = (TableLayout) findViewById(R.id.main_table);
JSONArray Jobj = new JSONArray(result);
for (int i = 0; i < Jobj.length(); i++) {
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setPadding(0, 14, 2, 14);
JSONObject news_preview = Jobj.getJSONObject(i);
Integer news_id = news_preview.getInt("id_articolo");
String news_title = news_preview.getString("titolo");
String news_image = news_preview.getString("immagine");
// Check if image url is relative or absolute
Pattern p = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
Matcher m = p.matcher(news_image);
if (m.matches() == false) {
url_img = path + news_image;
} else if (m.matches() == true) {
url_img = news_image;
}
// Call Html Parser to parse text
HtmlParser parsed_string = new HtmlParser();
Spanned title_nohtml = parsed_string.htmlparser(news_title);
// Thumb
ImageView img = new ImageView(getApplicationContext());
img.setAdjustViewBounds(true);
img.setMaxHeight(140);
img.setMaxWidth(140);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url_img).getContent());
img.setImageBitmap(bitmap);
LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// Clickable title
final TextView txt = new TextView(getApplicationContext());
txt.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
txt.setLayoutParams(params);
txt.setTextSize(18);
txt.setBackgroundColor(Color.WHITE);
txt.setTypeface(null, Typeface.BOLD);
txt.setTextColor(Color.BLACK);
txt.setId(news_id);
txt.setText(title_nohtml);
txt.setClickable(true);
row.addView(img);
row.addView(txt);
MainTable.addView(row);
txt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, OtherPages.class);
Bundle extras = new Bundle();
extras.putString("Boolean", "1");
extras.putInt("id_news", txt.getId());
intent.putExtras(extras);
startActivity(intent);
}
});
}
}catch (Exception e) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Si è verificato un errore");
alertDialog.setMessage("Errore 001" + "\n" + "Non è stato possibile soddisfare la tua richiesta, riprova più tardi.");
alertDialog.show();
}
progress.dismiss();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Solution
ondoInbackground
you want to do all the main tasks in the background ....
onPostexecute
you display all the results from the doinbackground
P.S : If u want to access to UI,do it in onPostexecute
or onProgressupdate
Answered By - Sukan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.