【发布时间】:2014-01-23 17:19:20
【问题描述】:
我有我的线程 UI。我需要下载信息来更新界面。 在我的 AsyncTask 中,我将数据放在我在 Json 中恢复的“程序”类中,并希望将这个类发送到我的线程 UI。但我真的不知道该怎么做。
我的 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";
}
}
主要:
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()));
> 编辑 1:
谢谢!
对不起,我对 Android 开发不是很擅长,我是在学校项目中发现的。
我现在可以在 Thread UI 中使用我的课程了!
这不仅是为了更新我的应用程序 Android 的界面,而且还使用在我的 Thread UI 中创建的 myWebsocket 在我的 webApp 中发送数据(我创建了另一个异步任务来执行并与之交互)。
各方: 1 => 在我的应用程序和 STB (SetTopBox) 之间订阅 ChannelID => 完成 => 将带有我的 ID 的处理程序消息发送到我的线程 UI
2 => 在我的 STB 中启动我的 WebApp => 完成 => 发送处理程序消息 OK
3 => 启动我的 WebSocket => 完成 => 发送处理程序消息 OK
4 => 获取 ChannelID WebApp => Finish => 将带有 ChannelID 的处理程序消息发送到我的线程 UI
5 => 获取当前 Canal/Media => Finish => 将带有我的 ID Canal 的处理程序消息发送到我的线程 UI
6 => 现在!获取媒体信息 => 使用接口将类程序发送到我的线程 UI
7 => 所以更新 Interface App 并启动最后一个异步任务以在我的 WebApp 中发送数据 如果有新节目,我记得 5/6/7
在我的第一个 asynctask 之后,我在我的 Handler Thread UI 中调用下一个,对于 Thread Ui(fragmentActivity) 和 Handler 来说并不太重??我的处理程序变大了。
> 编辑 2: 我将阅读有关 runOnUiThread 的信息
【问题讨论】:
标签: java android multithreading android-asynctask