I have my Thread UI. I need to download informations to update interface. In my AsyncTask, I put data in a class "Program" that i recover in Json and want to send this class to my Thread UI. But i don't Know really how to do that.
My AsyncTask is in outside of my main activity.
AsyncTask :
public class GetMediaInfo extends AsyncTask<String, String, String> {
private static final String LOG_TAG = Api.class.getSimpleName();
private Exception mException = null;
static String url_youtube_unik;
static Gson gson = null;
static String infoProgramDean = null, infoProgramTmdb = null, infoCanal = null, trailerYoutube = null, infoProgramBouygues = null;
String code;
String url;
Program program;
Canal canal;
private Handler handler;
public GetMediaInfo(Handler handler, Program program, Canal canal) {
this.handler = handler;
this.canal = canal;
this.program = program;
}
@Override
protected void onPreExecute () {
Log.v(LOG_TAG ,"PreExecute GetMediaInfo");
}
protected void onPostExecute (String result) {
Log.v(LOG_TAG ,"PostExecute GetMediaInfo");
//Message msgObj = handler.obtainMessage();
//Bundle b = new Bundle();
if (result != null) {
//b.putString("functionName", "UpdateInfoScreen");
//b.putString("message", "Update Info Program");
// HOW TO SEND MY Program to Thread Ui and update Interface ????????
} else {
Log.v(LOG_TAG, "Error getCurrentProgram : " + mException.getMessage());
//b.putString("functionName", "Message");
//b.putString("message", "Error getCurrentProgram : " + mException.getMessage());
}
// msgObj.setData(b);
// handler.sendMessage(msgObj);
}
@Override
protected String doInBackground(String... params) {
Log.v(LOG_TAG ,"doInBackground GetMediaInfo");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// Channel qu'on a récupéré
//String tvChannelId = canal.getId(); <=================================================== A DELETE avec STB
String tvChannelId = "1";
boolean found = false;
gson = new Gson();
String idProgram = null;
String nameProgram = null;
String typeProgram = null;
// --> Récupération Information du programme en cours + la liste du programme de la chaîne <--
// Prépare url bouygues Canal
url = "http://openbbox.flex.bouyguesbox.fr:81/V0/Media/EPG/Live?period=1&TVChannelsId=" + tvChannelId;
//On récupère information du program avec le site de Bouygues
try {
infoCanal = Api.getInfoMedia(url);
JSONObject myJson;
myJson = new JSONObject(infoCanal);
// "{\"id\": 51,\"nom\": \"Ma Chaîne Sport\", \"ListeProgrammes\": { \"Programme\": [{\"id\": 95347742,\"nom\": \"Bien-être\",\"description\": \"Emission du bien-être.\",\"debut\": \"2014-01-23T06:00:00Z\",\"fin\": \"2014-01-23T06:30:00Z\"}]}}";
idProgram = myJson.getJSONObject("ListeProgrammes").getJSONArray("Programme").getJSONObject(0).optString("id");
nameProgram = myJson.getJSONObject("ListeProgrammes").getJSONArray("Programme").getJSONObject(0).optString("nom");
Log.v("idProgram","= " + idProgram);
Log.v("nameProgram","= " + nameProgram);
} catch (ServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//FAKE ID Pour le TEST ID
idProgram = "95262486";
// Prépare url bouygues Info Program
url = "http://openbbox.flex.bouyguesbox.fr:81/V0/Media/EPG/Live?programId="+ idProgram;
try {
JSONObject myJson;
infoProgramBouygues = Api.getInfoMedia(url);
myJson = new JSONObject(infoProgramBouygues);
typeProgram = myJson.getJSONObject("Programme").getJSONObject("ListeGenres").optString("genre");
Log.v("genreProgram","= " + typeProgram);
// On ajoute heure de début et fin (format: "debut": "2014-01-22T20:45:00Z)
String beginningTime = myJson.getJSONObject("Programme").getJSONObject("Diffusion").optString("debut").substring(10);
String endTime = myJson.getJSONObject("Programme").getJSONObject("Diffusion").optString("fin").substring(10);
program.setBeginningTime(beginningTime);
program.setEndTime(endTime);
} catch (ServerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// GENRE == FILM
if (typeProgram.equals("Film")) {
// Remplace espaces par des +, puis ' par des
nameProgram = nameProgram.replaceAll("\\s","+").replaceAll("'", "'").replaceAll("&", "");
//nameProgram = "intouchables"; <============================================================================ A DELETE bon program
Log.v("nameProgram", "= " + nameProgram);
// Prépare URL DEAN
url = "http://deanclatworthy.com/imdb/?q="+nameProgram;
try {
infoProgramDean = Api.getInfoMedia(url);
JSONObject myJson;
myJson = new JSONObject(infoProgramDean);
code = null;
code = myJson.optString("code");
// Film trouvé
if(code.equals("")) {
String IdProgramIMDB = myJson.optString("imdbid");
Log.v("IdProgramIMDB", "= " + IdProgramIMDB);
// FAKE ID IMDB !!!! <============================================================= A DELETE
IdProgramIMDB = "tt0407304";
// Prépare URL TMDB
url = "http://api.themoviedb.org/3/movie/"+ IdProgramIMDB + "?api_key=a9cd33535bcdf6537692b5a25e5ef98e";
infoProgramTmdb = Api.getInfoMedia(url);
myJson = new JSONObject(infoProgramTmdb);
code = null;
code = myJson.optString("status_code");
// Film trouvé sur TMDB sinon on prend les informations de bouygues (code error 6)
if(code.equals("")) {
program = gson.fromJson(infoProgramTmdb, Program.class);
Log.v("runtimeProgram ", "= " + program.getRuntime());
Log.v("posterPathProgram ", "= " + program.getposterPath());
found = true;
}
}
nameProgram = "intouchables"; // FAKE Name Program !!!! <============================================================= A DELETE
// Récupération Youtube (5 vidéos trailers max)
url = "https://gdata.youtube.com/feeds/api/videos?q=" + nameProgram + "+trailer+movies&orderby=viewCount&start-index=1&max-results=5&v=2&alt=json";
trailerYoutube = Api.getInfoMedia(url);
//feed => {} => entry [] get(1 à 5) => id{} after last :
// Format Json { ..., "feed":{ ..., "entry":[{..., "id":{"$t":"tag:youtube.com,2008:video:17CLlZuiBkQ"}}, {"id":{...}}, {"id":{...}}, {"id":{...}}, {"id":{...}}]}}
myJson = new JSONObject(trailerYoutube);
if(!myJson.getJSONObject("feed").optString("entry").equals(""))
{
JSONArray dataYoutube = myJson.getJSONObject("feed").getJSONArray("entry");
//nameProgram = myJson.getJSONObject("ListeProgrammes").getJSONArray("Programme").getJSONObject(0).optString("nom");
for(int i=0;i<5;i++) {
String[] tag = dataYoutube.getJSONObject(i).getJSONObject("id").optString("$t").split(":");
//program.setYoutubeUri(new YoubuteURI(tag[tag.length-1])); // !!!! <============================================================= A ajoute dans la List Youtube de la classe program !!!!!
Log.v("idYoutube","= " + tag[tag.length-1]);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Si pas trouvé ou pas un film, on utilise information de Bouygues
if(found != true) {
Log.v("IdProgramIMDB","Not Found => BDD Bouygues");
//Log.v("programBouyges","Not Found => BDD Bouygues " + infoProgramBouygues);
JSONObject myJson;
try {
Log.v("bbbbbbbbb","= ");
myJson = new JSONObject(infoProgramBouygues);
String test = myJson.optString("Programme");
Log.v("aaaaaaaa","= " + infoProgramBouygues);
program = gson.fromJson(test, Program.class); // !!!! <============================================================= Ca ajoute pas dans la classe A VOIR !!!!!
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("TitleProgram ", "= " + program.getTitle());
}
// Utilisation des informations de la chaine + la liste de programme // !!!! <============================================================= Ca ajoute pas dans la classe A VOIR !!!!!
canal = gson.fromJson(infoCanal, Canal.class);
Log.v("NomCanal ", "= " + canal.getName()); // !!!! <============================================================= Voir si ca ajoute les valeurs ds la liste des programs!!!!!
}
return "youpi";
}
}
Main :
String canalId = msg.getData().getString("message");
currentCanal = new Canal(canalId);
currentProgram = new Program();
GetMediaInfo getMediaInfo = new GetMediaInfo(handler, currentProgram, currentCanal);
getMediaInfo.execute(NetworkUtils.getUrlHttp(getApplicationContext()));
> EDIT 1 :
Thank you !
i'm sorry, i'm not really good with Android development, I discovered with my school project.
i can use my class in the Thread UI now !
It was not just to update interface of my app Android but also send the data in my webApp with myWebsocket created in my Thread UI (i created another asynctask to do and interacts with).
The parties : 1 => Subscribe ChannelID between my app and STB (SetTopBox) => Finish => Send handler message with my ID to my Thread UI
2 => Start my WebApp in my STB => Finish => Send handler message OK
3 => Initiate my WebSocket => Finish => Send handler message OK
4 => Get ChannelID WebApp => Finish => Send handler message with ChannelID to my Thread UI
5 => Get Current Canal/Media => Finish => Send handler message with my ID Canal to my Thread UI
6 => Now ! Get Media Info => Send class program to my Thread UI with Interface
7 => So update Interface App and start a last asynctask to send data in my WebApp And I recall 5/6/7 if there is a new program
After my first asynctask, I call the next in my Handler Thread UI, it's not too heavy for Thread Ui(fragmentActivity) and Handler?? My handler become large.
> EDIT 2 : I will read Informations about runOnUiThread