Thursday, 12 September 2013

Interface

create class name MyInterface

public interface MyInterface {

public abstract  void Mydata(List<DataAll> listmy);
}

where you want list put below code

List<DataAll> Dataallobj;
BaseAdapter bas;

MyTask  task = new MyTask();
    task.setListeners(new MyInterface() {
    @Override
    public void Mydata(List<DataAll> listmy) {
    Dataallobj = listmy;
    mycontnamesetget = listmy;
    bas = new BaseAd(JitegaSlider.this);
    MyHandler.sendEmptyMessage(COMPLETEMAIN);
    }
    });

create class MyTask


public class MyTask  extends  AsyncTask<String,Void, List<DataAll>>
{
private static final String  TAG = "MyTask -";
List<DataAll> Dataallobj = new ArrayList<DataAll>();
MyInterface  objedatasend;
String Response;
boolean temp = true;
InputStream is = null;
 
@Override
protected List<DataAll> doInBackground(String... params) {
Log.i(General.TAG,TAG+"doInBackground -");
List<DataAll> Dataallobj = new ArrayList<DataAll>();
for (String url : params) 
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
Response = "";
try {
HttpResponse execute = client.execute(httpGet);

InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content),8);
String s = "";
while ((s = buffer.readLine()) != null) {
Response += s;
Log.i(General.TAG, "get response....."+Response);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
JSONObject json = new JSONObject(Response.replaceAll("null", ""));
JSONObject jobj = json.getJSONObject("feed");
JSONArray jsonArray = jobj.getJSONArray("entry");
String Title = "";
String Vidio = "";
String Imageurl = "";
String Summary = "";
String ImgHeight = "";
String ImgWidth = "";
String Imgname = "";
String favouritecount = "";
String viewcount = "";
String numDislike = "0";
String numLike = "0";
String currentDateandTime = "";
String videotimedur = "";
String mytime = "";
JSONObject jobtitle = null,jobcontent=null,jobstatistic=null,jobjmedia = null,jobmediadesc=null,jobjrating=null,jobdate = null,jobvideotime = null;
if(jsonArray != null){
for (int i = 0; i < jsonArray.length(); i++) 
{
JSONObject Entryobj = jsonArray.getJSONObject(i);
// This Array For Getting ImageUrl                     
if(Entryobj.has("published")){
jobdate = Entryobj.getJSONObject("published");
}
if(Entryobj.has("title")){
jobtitle = Entryobj.getJSONObject("title");
}
if(Entryobj.has("yt$statistics")){
//null
jobstatistic = Entryobj.getJSONObject("yt$statistics");
}
if(Entryobj.has("media$group")){
jobjmedia = Entryobj.getJSONObject("media$group");
}
if(jobjmedia.has("media$description")){
jobmediadesc = jobjmedia.getJSONObject("media$description");
}
if(jobjmedia.has("yt$videoid")){
jobcontent = jobjmedia.getJSONObject("yt$videoid");
}
if(jobjmedia.has("yt$duration")){
jobvideotime = jobjmedia.getJSONObject("yt$duration");
}
if(Entryobj.has("yt$rating")){
//null
jobjrating = Entryobj.getJSONObject("yt$rating");
}
JSONArray MediaThumbnailArray = jobjmedia.getJSONArray("media$thumbnail");
for (int m = 0; m < MediaThumbnailArray.length(); m++) {
JSONObject MediaImg = MediaThumbnailArray.getJSONObject(m);
ImgHeight = MediaImg.getString("height");
if(ImgHeight.equalsIgnoreCase("360")){
Imageurl = MediaImg.getString("url");
}
ImgWidth = MediaImg.getString("width");
Imgname = MediaImg.getString("yt$name");
Log.i(TAG, "get image....."+Imageurl);
Log.i(TAG, "get ImgHeight....."+ImgHeight);
Log.i(TAG, "get ImgWidth....."+ImgWidth);
Log.i(TAG, "get Imgname....."+Imgname);
}
if(jobdate != null){
if(jobdate.has("$t")){
currentDateandTime = jobdate.getString("$t");
Log.i(TAG, "get currentDateandTime....."+currentDateandTime);
}
}
if(jobtitle != null){
if(jobtitle.has("$t")){
Title = jobtitle.getString("$t");
Log.i(General.TAG, "get title...."+Title);
}
}
if(jobmediadesc != null){
if(jobmediadesc.has("$t")){
Summary = jobmediadesc.getString("$t");
Log.i(General.TAG, "get Summary...."+Summary);
}
}
if(jobcontent != null){
if(jobcontent.has("$t")){
Vidio =  jobcontent.getString("$t");
Log.i(General.TAG, "get Vidio url...."+Vidio);
}
}
if(jobvideotime != null){
if(jobvideotime.has("seconds")){
videotimedur =  jobvideotime.getString("seconds");
Log.i(General.TAG, "get Vidio time in seconds...."+Vidio);
 mytime = getDurationString(Integer.parseInt(videotimedur));
        Log.i(TAG, "get mytime..."+mytime);
}
}
if(jobstatistic != null){
if(jobstatistic.has("favoriteCount")){
favouritecount = jobstatistic.getString("favoriteCount");
Log.i(General.TAG, "get favouritecount...."+favouritecount);
}
if(jobstatistic.has("viewCount")){
viewcount = jobstatistic.getString("viewCount");
Log.i(General.TAG, "get viewcount...."+viewcount);
}
}
if(jobjrating != null){
if(jobjrating.has("numDislikes")){
numDislike = jobjrating.getString("numDislikes");
Log.i(General.TAG, "get numDislike...."+numDislike);
}
if(jobjrating.has("numLikes")){
numLike = jobjrating.getString("numLikes");
Log.i(General.TAG, "get numLike...."+numLike);
}
}
Dataallobj.add(new DataAll(Title,Summary,Vidio,Imageurl,ImgHeight,ImgWidth,Imgname,favouritecount,viewcount,numDislike,numLike,currentDateandTime,mytime));
}}
} catch (Exception e) {
System.out.println("" + e.toString());
Log.i(General.TAG,TAG +" To Parse Json Data On BackGroundMehtod Errore");
}
}
return Dataallobj;
}

@Override
protected void onPostExecute(List<DataAll> result) {
Log.i(General.TAG,TAG+"onPostExecute - Result: "+result);
super.onPostExecute(result);  
objedatasend.Mydata(result);
 
}


public void setListeners(MyInterface  datasend)
{
 Log.i(General.TAG,TAG+" setListeners -");
 objedatasend = datasend;  
}


private String getDurationString(int seconds) {

    int hours = seconds / 3600;
    int minutes = (seconds % 3600) / 60;
    seconds = seconds % 60;

    return twoDigitString(hours) + " : " + twoDigitString(minutes) + " : " + twoDigitString(seconds);
}

private String twoDigitString(int number) {

    if (number == 0) {
        return "00";
    }

    if (number / 10 == 0) {
        return "0" + number;
    }

    return String.valueOf(number);
}

}

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...