I have one imageview, i want to update it after 15sec with a new image which is stored in url, ex. "http://example.com". i also used picasso to update but the imageview is not updating, I can fetch Image from server first time when activity starts, but I don't know how to fetch random images from server and update the same.
Here is my AsyncTask code
public class MainActivity extends AppCompatActivity {
GiraffePlayer player;
String str_ad_id, str_ad_image, str_id_name, url,stringId;
ProgressBar progressBar;
ImageView adImageView;
List<AdItem> adItemList;
List<AdItem2> adList;
AdItem object;
AdItem2 object1;
List<PresentIds> presentIdList;
PresentIds presentIdObject;
ArrayList<Integer> array_present_ids;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player = new GiraffePlayer(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
url = "****";
adImageView = (ImageView) findViewById(R.id.adImage);
adItemList = new ArrayList<AdItem>();
adList = new ArrayList<AdItem2>();
presentIdList = new ArrayList<PresentIds>();
array_present_ids = new ArrayList<Integer>();
player.play(url);
if (JsonUtils.isNetworkAvailable(MainActivity.this)) {
new Add_Main_Task().execute(Config.SERVER_URL + "/add_api.php?advertise/");
}
else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
player.onComplete(new Runnable() {
@Override
public void run() {
//callback when video is finish
Toast.makeText(getApplicationContext(), "video play completed",Toast.LENGTH_SHORT).show();
}
}).onInfo(new GiraffePlayer.OnInfoListener() {
@Override
public void onInfo(int what, int extra) {
switch (what) {
case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
//do something when buffering start
break;
case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
//do something when buffering end
break;
case IMediaPlayer.MEDIA_INFO_NETWORK_BANDWIDTH:
//download speed
((TextView) findViewById(R.id.tv_speed)).setText(Formatter.formatFileSize(getApplicationContext(), extra) + "/s");
break;
case IMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
//do something when video rendering
findViewById(R.id.tv_speed).setVisibility(View.GONE);
break;
}
}
}).onError(new GiraffePlayer.OnErrorListener() {
@Override
public void onError(int what, int extra) {
Toast.makeText(getApplicationContext(), "video play error", Toast.LENGTH_SHORT).show();
}
});
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
if (v.getId() == R.id.btn_start) {
player.start();
findViewById(R.id.btn_pause).setVisibility(View.VISIBLE);
findViewById(R.id.btn_start).setVisibility(View.INVISIBLE);
findViewById(R.id.btn_start).setVisibility(View.GONE);
}
else if (v.getId() == R.id.btn_pause) {
player.pause();
findViewById(R.id.btn_start).setVisibility(View.VISIBLE);
findViewById(R.id.btn_pause).setVisibility(View.INVISIBLE);
findViewById(R.id.btn_pause).setVisibility(View.GONE);
}else if (v.getId() == R.id.btn_toggle) {
player.toggleFullScreen();
}
new PresentAds().execute(Config.SERVER_URL_MUKTHA + "/add_id.php?ad_id/");
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
performBackgroundTask.execute(Config.SERVER_URL + "/add_api.php?advertise/");*/
adImageView.setImageResource(android.R.color.transparent);
new MyMainTask().execute(Config.SERVER_URL + "/add_api.php?add_id="+stringId);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 10000, 10000);
}
private class PresentAds extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
return JsonUtils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
/*if (null != progressDialog && progressDialog.isShowing()) {
progressDialog.dismiss();
}
*/
if (null == result || result.length() == 0) {
Toast.makeText(MainActivity.this, "network", Toast.LENGTH_SHORT).show();
} else {
try {
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray = mainJson.getJSONArray(JsonConfig.ID_ARRAY_NAME);
JSONObject objJson = null;
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
PresentIds objItem = new PresentIds();
objItem.setPresentId(objJson.getString(JsonConfig.AD_MAIN_ID));
presentIdList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int j = 0; j < presentIdList.size(); j++) {
presentIdObject = presentIdList.get(j);
array_present_ids.add(Integer.parseInt(presentIdObject.getPresentId()));
/*str_present_ids = array_present_ids.toArray(str_present_ids);*/
}
Random r = new Random();
int id = array_present_ids.get(r.nextInt(array_present_ids.size()));
stringId = String.valueOf(id);
JsonConfig.AD_ID = stringId;
/*Toast.makeText(MainActivity.this, "id = "+stringId, Toast.LENGTH_SHORT).show();*/
callAsynchronousTask();
}
}
}
private class MyMainTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
return JsonUtils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressBar.setVisibility(View.GONE);
if (null == result || result.length() == 0) {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
} else {
try {
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray = mainJson.getJSONArray(JsonConfig.AD_ARRAY_NAME);
JSONObject objJson = null;
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
AdItem2 objItem = new AdItem2();
objItem.setAd_Id(objJson.getString(JsonConfig.AD_MAIN_ID));
objItem.setAd_Image(objJson.getString(JsonConfig.AD_IMAGE));
objItem.setAd_Name(objJson.getString(JsonConfig.AD_NAME));
objItem.setAd_Date(objJson.getString(JsonConfig.AD_DATE));
adList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
refreshImageView();
}
}
}
private class Add_Main_Task extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
adImageView.invalidate();
}
@Override
protected String doInBackground(String... params) {
return JsonUtils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressBar.setVisibility(View.GONE);
adImageView.setVisibility(View.VISIBLE);
if (null == result || result.length() == 0) {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
adImageView.setVisibility(View.GONE);
} else {
try {
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray = mainJson.getJSONArray(JsonConfig.AD_ARRAY_NAME);
JSONObject objJson = null;
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
AdItem objItem = new AdItem();
objItem.setAd_Id(objJson.getString(JsonConfig.AD_MAIN_ID));
objItem.setAd_Name(objJson.getString(JsonConfig.AD_NAME));
objItem.setAd_Image(objJson.getString(JsonConfig.AD_IMAGE));
adItemList.add(objItem);
}
}
catch (JSONException e) {
e.printStackTrace();
}
setAddToImageView();
}
}
}
public void setAddToImageView() {
object = adItemList.get(0);
str_ad_id = object.getAd_Id();
/* str_id_name = object.getAd_Name();*/
str_ad_image = object.getAd_Image();
url = Config.SERVER_URL +"/upload/add/"+object.getAd_Image();
Picasso.with(MainActivity.this).load(url).into(adImageView);
}
public void refreshImageView() {
object1 = adList.get(0);
str_ad_id = object1.getAd_Id();
/* str_id_name = object.getAd_Name();*/
str_ad_image = object1.getAd_Image();
url = Config.SERVER_URL +"/upload/add/"+object1.getAd_Image();
both picasso used but imageview is not updating.
Picasso picasso = Picasso.with(MainActivity.this);
picasso.invalidate(url);
picasso.load(url).into(adImageView);
Picasso.with(this).load(url) .memoryPolicy(MemoryPolicy.NO_CACHE) .networkPolicy(NetworkPolicy.NO_CACHE) .into(adImageView);
}
Aucun commentaire:
Enregistrer un commentaire