diff --git a/app/src/main/java/com/hi/music/player/api/RequestListener.java b/app/src/main/java/com/hi/music/player/api/RequestListener.java index 79f7c9c..8f452cb 100644 --- a/app/src/main/java/com/hi/music/player/api/RequestListener.java +++ b/app/src/main/java/com/hi/music/player/api/RequestListener.java @@ -1,9 +1,5 @@ package com.hi.music.player.api; -import com.hi.music.player.javabean.response.BaseResponse; - -import java.io.IOException; - public interface RequestListener { void onFail(String errorMsg); diff --git a/app/src/main/java/com/hi/music/player/javabean/requestbody/BodyHome.java b/app/src/main/java/com/hi/music/player/javabean/requestbody/BodyHome.java index 25bbe53..81c678e 100644 --- a/app/src/main/java/com/hi/music/player/javabean/requestbody/BodyHome.java +++ b/app/src/main/java/com/hi/music/player/javabean/requestbody/BodyHome.java @@ -13,16 +13,41 @@ public class BodyHome implements Serializable { private Context context = new Context(); - - private class Context implements Serializable{ - private Client client = new Client(); + public Context getContext() { + return context; } - private class Client implements Serializable{ + public void setContext(Context context) { + this.context = context; + } + + public class Context implements Serializable{ + private Client client = new Client(); + + public Client getClient() { + return client; + } + + public void setClient(Client client) { + this.client = client; + } + } + + public class Client implements Serializable{ private String clientName = "WEB_REMIX"; private String clientVersion = "1.20220918"; private String hl = Locale.getDefault().getLanguage(); private String gl = "US"; private String platform = "DESKTOP"; + + private String visitorData; + + public String getVisitorData() { + return visitorData; + } + + public void setVisitorData(String visitorData) { + this.visitorData = visitorData; + } } } diff --git a/app/src/main/java/com/hi/music/player/javabean/response/BaseResponse.java b/app/src/main/java/com/hi/music/player/javabean/response/BaseResponse.java deleted file mode 100644 index 830b0bc..0000000 --- a/app/src/main/java/com/hi/music/player/javabean/response/BaseResponse.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.hi.music.player.javabean.response; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - - -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.IOException; - -import okhttp3.MediaType; - -import okio.BufferedSource; -import retrofit2.Response; - -public class BaseResponse { - -// public JSONObject toJsonObject() { -// try { -// String string = this.string(); -// return new JSONObject(string); -// } catch (IOException | JSONException exception) { -// return null; -// } -// } - - - - - -} diff --git a/app/src/main/java/com/hi/music/player/javabean/response/ResponseCategory.java b/app/src/main/java/com/hi/music/player/javabean/response/ResponseCategory.java new file mode 100644 index 0000000..ba2b2c9 --- /dev/null +++ b/app/src/main/java/com/hi/music/player/javabean/response/ResponseCategory.java @@ -0,0 +1,37 @@ +package com.hi.music.player.javabean.response; + +public class ResponseCategory { + + //类别封面 + private String covert; + //类别标题 + private String twoTitle; + + //类别描述 + private String twoSubtitle; + + + public String getCovert() { + return covert; + } + + public void setCovert(String covert) { + this.covert = covert; + } + + public String getTwoTitle() { + return twoTitle; + } + + public void setTwoTitle(String twoTitle) { + this.twoTitle = twoTitle; + } + + public String getTwoSubtitle() { + return twoSubtitle; + } + + public void setTwoSubtitle(String twoSubtitle) { + this.twoSubtitle = twoSubtitle; + } +} diff --git a/app/src/main/java/com/hi/music/player/javabean/response/ResponseHome.java b/app/src/main/java/com/hi/music/player/javabean/response/ResponseHome.java index 4ba5755..35c9983 100644 --- a/app/src/main/java/com/hi/music/player/javabean/response/ResponseHome.java +++ b/app/src/main/java/com/hi/music/player/javabean/response/ResponseHome.java @@ -1,5 +1,67 @@ package com.hi.music.player.javabean.response; +import androidx.annotation.Nullable; + +import java.util.List; + public class ResponseHome { + + + //用于更多数据请求的cit + private String clickTrackingParams; + + //用于更多数据请求的ctoken + private String continuation; + + + @Nullable + //用于更多数据请求的visitorData(只有第一个接口会返回该值) + private String visitorData; + + @Nullable private String backgroundUrl; + private List childList; + + + public List getChildList() { + return childList; + } + + public void setChildList(List childList) { + this.childList = childList; + } + + public String getClickTrackingParams() { + return clickTrackingParams; + } + + public void setClickTrackingParams(String clickTrackingParams) { + this.clickTrackingParams = clickTrackingParams; + } + + public String getContinuation() { + return continuation; + } + + public void setContinuation(String continuation) { + this.continuation = continuation; + } + + public String getVisitorData() { + return visitorData; + } + + public void setVisitorData(String visitorData) { + this.visitorData = visitorData; + } + + public String getBackgroundUrl() { + return backgroundUrl; + } + + public void setBackgroundUrl(String backgroundUrl) { + this.backgroundUrl = backgroundUrl; + } + + } diff --git a/app/src/main/java/com/hi/music/player/javabean/response/ResponseHomeChild.java b/app/src/main/java/com/hi/music/player/javabean/response/ResponseHomeChild.java new file mode 100644 index 0000000..3c01e6f --- /dev/null +++ b/app/src/main/java/com/hi/music/player/javabean/response/ResponseHomeChild.java @@ -0,0 +1,36 @@ +package com.hi.music.player.javabean.response; + +import java.util.List; + +public class ResponseHomeChild { + + private String HeaderTitle; + + private List singleList; + private List categoryList; + + + public String getHeaderTitle() { + return HeaderTitle; + } + + public void setHeaderTitle(String headerTitle) { + HeaderTitle = headerTitle; + } + + public List getSingleList() { + return singleList; + } + + public void setSingleList(List singleList) { + this.singleList = singleList; + } + + public List getCategoryList() { + return categoryList; + } + + public void setCategoryList(List categoryList) { + this.categoryList = categoryList; + } +} diff --git a/app/src/main/java/com/hi/music/player/javabean/response/ResponseSingle.java b/app/src/main/java/com/hi/music/player/javabean/response/ResponseSingle.java new file mode 100644 index 0000000..a601318 --- /dev/null +++ b/app/src/main/java/com/hi/music/player/javabean/response/ResponseSingle.java @@ -0,0 +1,47 @@ +package com.hi.music.player.javabean.response; + +public class ResponseSingle { + + //歌手头像 + private String SingerHead; + //歌曲名字 + private String SongTitle; + + //歌手名字 + private String SingerName; + //描述 + private String Description; + + + public String getSingerHead() { + return SingerHead; + } + + public void setSingerHead(String singerHead) { + SingerHead = singerHead; + } + + public String getSongTitle() { + return SongTitle; + } + + public void setSongTitle(String songTitle) { + SongTitle = songTitle; + } + + public String getSingerName() { + return SingerName; + } + + public void setSingerName(String singerName) { + SingerName = singerName; + } + + public String getDescription() { + return Description; + } + + public void setDescription(String description) { + Description = description; + } +} diff --git a/app/src/main/java/com/hi/music/player/network/JsonHelper.java b/app/src/main/java/com/hi/music/player/network/JsonHelper.java index 349e4d1..a46e818 100644 --- a/app/src/main/java/com/hi/music/player/network/JsonHelper.java +++ b/app/src/main/java/com/hi/music/player/network/JsonHelper.java @@ -1,86 +1,46 @@ package com.hi.music.player.network; import com.hi.music.player.helper.CommonUtils; +import com.hi.music.player.javabean.response.ResponseCategory; +import com.hi.music.player.javabean.response.ResponseHome; +import com.hi.music.player.javabean.response.ResponseHomeChild; +import com.hi.music.player.javabean.response.ResponseSingle; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.util.ArrayList; +import java.util.List; + public class JsonHelper { - - public static void ResolveHomeJson(JSONObject jsonObject){ + public static ResponseHome ResolveHomeJson(JSONObject jsonObject) { + ResponseHome responseHome = new ResponseHome(); try { - String bgUrl = jsonObject - .getJSONObject("background") - .getJSONObject("musicThumbnailRenderer") - .getJSONObject("thumbnail") - .getJSONArray("thumbnails") - .getJSONObject(0) - .getString("url"); + String bgUrl = getJsonUrl(jsonObject + .getJSONObject("background")); - JSONArray contents = jsonObject + JSONObject sectionListRenderer = jsonObject .getJSONObject("contents") .getJSONObject("singleColumnBrowseResultsRenderer") .getJSONArray("tabs") .getJSONObject(0) .getJSONObject("tabRenderer") .getJSONObject("content") - .getJSONObject("sectionListRenderer") - .getJSONArray("contents"); + .getJSONObject("sectionListRenderer"); + JSONObject responseContext = jsonObject .getJSONObject("responseContext"); JSONArray serviceTrackingParams = responseContext.getJSONArray("serviceTrackingParams"); String visitorData = responseContext.getString("visitorData"); + CommonUtils.LogMsg("----------visitorData=" + visitorData); - for (int i = 0; i < contents.length(); i++) { - JSONObject object = contents.getJSONObject(i); + responseHome.setBackgroundUrl(bgUrl); + responseHome.setVisitorData(visitorData); + getCommonHome(sectionListRenderer, responseHome); - JSONObject musicCarouselShelfRenderer = object.optJSONObject("musicCarouselShelfRenderer"); - if(musicCarouselShelfRenderer!= null){ - String title = musicCarouselShelfRenderer - .getJSONObject("header") - .getJSONObject("musicCarouselShelfBasicHeaderRenderer") - .getJSONObject("title") - .getJSONArray("runs") - .getJSONObject(0) - .getString("text"); - CommonUtils.LogMsg("----------headertitle=" + title); - JSONArray childContents = musicCarouselShelfRenderer - .getJSONArray("contents"); - - - for (int j = 0; j < childContents.length(); j++) { - JSONObject jsonList = childContents.getJSONObject(j); - JSONObject musicResponsiveListItemRenderer = jsonList.optJSONObject("musicResponsiveListItemRenderer"); - if(musicResponsiveListItemRenderer!= null){ - String covert = musicResponsiveListItemRenderer - .getJSONObject("thumbnail") - .getJSONObject("musicThumbnailRenderer") - .getJSONObject("thumbnail") - .getJSONArray("thumbnails") - .getJSONObject(0) - .getString("url"); - - String text = musicResponsiveListItemRenderer.getJSONArray("flexColumns") - .getJSONObject(0) - .getJSONObject("musicResponsiveListItemFlexColumnRenderer") - .getJSONObject("text") - .getJSONArray("runs") - .getJSONObject(0) - .getString("text"); - - CommonUtils.LogMsg(" --------------text=" + text+"-covert="+covert); - } - } - } - - - - - - } } catch (JSONException exception) { @@ -88,5 +48,163 @@ public class JsonHelper { exception.printStackTrace(); } + + return responseHome; + } + + + public static ResponseHome ResolveHomeMoreJson(JSONObject jsonObject) { + ResponseHome responseHome = new ResponseHome(); + try { + + JSONObject sectionListContinuation = jsonObject + .getJSONObject("continuationContents") + .getJSONObject("sectionListContinuation"); + + + JSONObject responseContext = jsonObject + .getJSONObject("responseContext"); + JSONArray serviceTrackingParams = responseContext.getJSONArray("serviceTrackingParams"); + + getCommonHome(sectionListContinuation, responseHome); + + } catch (JSONException exception) { + CommonUtils.LogMsg("----------exception="); + exception.printStackTrace(); + + } + return responseHome; + } + + + private static void getCommonHome(JSONObject sectionListRenderer, ResponseHome responseHome) throws JSONException { + + + JSONObject nextContinuationData = sectionListRenderer + .getJSONArray("continuations") + .getJSONObject(0) + .getJSONObject("nextContinuationData"); + + //token=continuation,cit= clickTrackingParams + + String clickTrackingParams = nextContinuationData.getString("clickTrackingParams"); + String continuation = nextContinuationData.getString("continuation"); + CommonUtils.LogMsg("----------clickTrackingParams=" + clickTrackingParams); + CommonUtils.LogMsg("----------continuation=" + continuation); + responseHome.setClickTrackingParams(clickTrackingParams); + responseHome.setContinuation(continuation); + + JSONArray contents = sectionListRenderer + .getJSONArray("contents"); + + List childList = new ArrayList<>(); + + for (int i = 0; i < contents.length(); i++) { + ResponseHomeChild responseHomeChild = new ResponseHomeChild(); + JSONObject object = contents.getJSONObject(i); + + JSONObject musicCarouselShelfRenderer = object.optJSONObject("musicCarouselShelfRenderer"); + if (musicCarouselShelfRenderer != null) { + + //模块标题 + String title = getJsonText(musicCarouselShelfRenderer + .getJSONObject("header") + .getJSONObject("musicCarouselShelfBasicHeaderRenderer") + .getJSONObject("title")); + CommonUtils.LogMsg("----------headertitle=" + title); + responseHomeChild.setHeaderTitle(title); + JSONArray childContents = musicCarouselShelfRenderer + .getJSONArray("contents"); + + + List categoryList = new ArrayList<>(); + List singleList = new ArrayList<>(); + for (int j = 0; j < childContents.length(); j++) { + JSONObject jsonList = childContents.getJSONObject(j); + JSONObject musicResponsiveListItemRenderer = jsonList.optJSONObject("musicResponsiveListItemRenderer"); + if (musicResponsiveListItemRenderer != null) { + ResponseSingle responseSingle = new ResponseSingle(); + + String SingerHead = getJsonUrl(musicResponsiveListItemRenderer + .getJSONObject("thumbnail")); + + + JSONArray flexColumns = musicResponsiveListItemRenderer.getJSONArray("flexColumns"); + + //歌名、歌手名字、描述 + String SongTitle = ""; + String SingerName = ""; + String Description = ""; + for (int g = 0; g < flexColumns.length(); g++) { + String text = getJsonText(musicResponsiveListItemRenderer.getJSONArray("flexColumns") + .getJSONObject(g) + .getJSONObject("musicResponsiveListItemFlexColumnRenderer") + .getJSONObject("text")); + if (g == 0) SongTitle = text; + if (g == 1) SingerName = text; + if (g == 2) Description = text; + } + responseSingle.setSongTitle(SongTitle); + responseSingle.setSingerName(SingerName); + responseSingle.setSingerHead(SingerHead); + responseSingle.setDescription(Description); + singleList.add(responseSingle); + CommonUtils.LogMsg(" ----------111111111111----SongTitle=" + SongTitle + "--SingerName=" + SingerName + "---Description=" + Description + "--SingerHead=" + SingerHead); + } + + JSONObject musicTwoRowItemRenderer = jsonList.optJSONObject("musicTwoRowItemRenderer"); + if (musicTwoRowItemRenderer != null) { + ResponseCategory responseCategory = new ResponseCategory(); + String covert = getJsonUrl(musicTwoRowItemRenderer + .getJSONObject("thumbnailRenderer")); + + String twoTitle = getJsonText(musicTwoRowItemRenderer.getJSONObject("title")); + String twoSubtitle = getJsonText(musicTwoRowItemRenderer.getJSONObject("subtitle")); + responseCategory.setCovert(covert); + responseCategory.setTwoTitle(twoTitle); + responseCategory.setTwoSubtitle(twoSubtitle); + categoryList.add(responseCategory); + CommonUtils.LogMsg(" ----------2222222222----twoTitle=" + twoTitle + "-twoSubtitle=" + twoSubtitle + "---封面=" + covert); + } + + } + responseHomeChild.setCategoryList(categoryList); + responseHomeChild.setSingleList(singleList); + } + childList.add(responseHomeChild); + } + responseHome.setChildList(childList); + + + } + + private static String getJsonUrl(JSONObject jsonObject) { + try { + JSONArray jsonArray = jsonObject.getJSONObject("musicThumbnailRenderer") + .getJSONObject("thumbnail") + .getJSONArray("thumbnails"); + + int length = jsonArray.length(); + CommonUtils.LogMsg("----------length=" + (length - 1)); + String pngUrl = jsonArray.getJSONObject(length - 1) + .getString("url"); + + return pngUrl; + } catch (JSONException exception) { + return ""; + } + } + + + private static String getJsonText(JSONObject jsonObject) { + try { + String text = jsonObject.getJSONArray("runs") + .getJSONObject(0) + .getString("text"); + + return text; + } catch (JSONException exception) { + return ""; + } } } diff --git a/app/src/main/java/com/hi/music/player/network/MusicApi.java b/app/src/main/java/com/hi/music/player/network/MusicApi.java index 0fcf012..f65743f 100644 --- a/app/src/main/java/com/hi/music/player/network/MusicApi.java +++ b/app/src/main/java/com/hi/music/player/network/MusicApi.java @@ -1,13 +1,12 @@ package com.hi.music.player.network; -import com.hi.music.player.javabean.response.BaseResponse; import io.reactivex.Observable; import okhttp3.RequestBody; import okhttp3.ResponseBody; -import retrofit2.Response; import retrofit2.http.Body; import retrofit2.http.Headers; import retrofit2.http.POST; +import retrofit2.http.Query; public interface MusicApi { @@ -16,5 +15,20 @@ public interface MusicApi { Observable getHomeData(@Body RequestBody requestBody); +// "youtubei/v1/browse?ctoken=${baseHomePage.cToken}&continuation=${baseHomePage.continuation}&type=next&itct=${baseHomePage.itct}&prettyPrint=false" + + @POST("youtubei/v1/browse") + @Headers("X-Goog-Api-Key:AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8") + Observable getHomeMoreData(@Query("ctoken") String token, + @Query("continuation") String continuation, + @Query("type") String type, + @Query("itct") String itct, + @Query("prettyPrint") boolean prettyPrint,@Body RequestBody requestBody); + + +// @POST("youtubei/v1/browse") +// @Headers("X-Goog-Api-Key:AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8") +// @FormUrlEncoded +// Observable getHomeMoreData(@FieldMap Map map); } diff --git a/app/src/main/java/com/hi/music/player/network/ObserverWrapper.java b/app/src/main/java/com/hi/music/player/network/ObserverWrapper.java index a518961..e98594d 100644 --- a/app/src/main/java/com/hi/music/player/network/ObserverWrapper.java +++ b/app/src/main/java/com/hi/music/player/network/ObserverWrapper.java @@ -2,7 +2,6 @@ package com.hi.music.player.network; import com.hi.music.player.api.RequestListener; import com.hi.music.player.helper.CommonUtils; -import com.hi.music.player.javabean.response.BaseResponse; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; diff --git a/app/src/main/java/com/hi/music/player/network/RetrofitManager.java b/app/src/main/java/com/hi/music/player/network/RetrofitManager.java index 780d1fb..65b3357 100644 --- a/app/src/main/java/com/hi/music/player/network/RetrofitManager.java +++ b/app/src/main/java/com/hi/music/player/network/RetrofitManager.java @@ -1,17 +1,14 @@ package com.hi.music.player.network; import com.google.gson.Gson; -import com.google.gson.JsonIOException; import com.hi.music.player.api.RequestListener; import com.hi.music.player.helper.CommonUtils; import com.hi.music.player.javabean.requestbody.BodyHome; -import com.hi.music.player.javabean.response.BaseResponse; +import com.hi.music.player.javabean.response.ResponseHome; -import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; -import java.io.IOException; +import java.util.HashMap; import java.util.concurrent.TimeUnit; import io.reactivex.android.schedulers.AndroidSchedulers; @@ -81,7 +78,6 @@ public class RetrofitManager { Gson gson = new Gson(); String s = gson.toJson(bodyHome); RequestBody requestBody = RequestBody.Companion.create(s, JSON); -// RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), s); musicApi.getHomeData(requestBody) .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) @@ -97,12 +93,47 @@ public class RetrofitManager { public void onSuccess(ResponseBody data) { JSONObject jsonObject = CommonUtils.toJsonObject(data); if (jsonObject != null) { - JsonHelper.ResolveHomeJson(jsonObject); + ResponseHome responseHome = JsonHelper.ResolveHomeJson(jsonObject); + CommonUtils.LogMsg(""); } } })); } + public void getHomeMoreData(String continuation,String itct,String visitorData) { + BodyHome bodyHome = new BodyHome(); + bodyHome.getContext().getClient().setVisitorData(visitorData); + Gson gson = new Gson(); + String s = gson.toJson(bodyHome); + RequestBody requestBody = RequestBody.Companion.create(s, JSON); + HashMap stringHashMap = new HashMap<>(); + stringHashMap.put("ctoken",continuation); + stringHashMap.put("continuation",continuation); + stringHashMap.put("type","next"); + stringHashMap.put("itct",itct); + stringHashMap.put("prettyPrint","false"); + musicApi.getHomeMoreData(continuation,continuation,"next",itct,false,requestBody) + .subscribeOn(Schedulers.io()) + .unsubscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new ObserverWrapper(new RequestListener() { + + @Override + public void onFail(String errorMsg) { + + } + + @Override + public void onSuccess(ResponseBody data) { + JSONObject jsonObject = CommonUtils.toJsonObject(data); + if (jsonObject != null) { + ResponseHome responseHome = JsonHelper.ResolveHomeMoreJson(jsonObject); + CommonUtils.LogMsg(""); + } + + } + })); + } } diff --git a/app/src/main/java/com/hi/music/player/ui/MainActivity.java b/app/src/main/java/com/hi/music/player/ui/MainActivity.java index a7b64d2..891cc47 100644 --- a/app/src/main/java/com/hi/music/player/ui/MainActivity.java +++ b/app/src/main/java/com/hi/music/player/ui/MainActivity.java @@ -9,7 +9,7 @@ import com.hi.music.player.ui.BaseActivity; public class MainActivity extends BaseActivity { -// EdgeToEdge.enable(this); + // EdgeToEdge.enable(this); @Override protected ActivityMainBinding getViewBinding() { return ActivityMainBinding.inflate(getLayoutInflater()); @@ -20,10 +20,21 @@ public class MainActivity extends BaseActivity { vb.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + RetrofitManager.getInstance().getHomeData(); } }); + vb.tv1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String continuation = "4qmFsgKrAhIMRkVtdXNpY19ob21lGpoCQ0FONnpBRkhVSGswZFMxUVIzazBaMFJYYjFWQ1EyOUpRa05wVWpWa1JqbDNXVmRrYkZnelRuVlpXRUo2WVVjNU1GZ3lNVEZqTW14cVdETkNhRm95Vm1aamJWWnVZVmM1ZFZsWGQxTklNV3Q1V2tSUmRHRldUa2RoTUZKS1ZteEdSR0ZXVG5GbFIzaERWVlZvTVZORlVYUlVXR3hLVVcxellVOVZNVEZqTW14cVVrZHNlbGt5T1RKYVdFbzFWVWRHYmxwV1RteGpibHB3V1RKVmRGSXlWakJUUnpsMFdsWkNhRm95VlVGQlVVSTJZVU14UkZSblFVSldWazFCUVZaV1ZFRkJSVUpCWDNGamVEY3dTa0ZuWjBVJTNE"; + String clickTrackingParams = "CBAQybcCIhMIw4eu48bLiAMVvoDkBh2lGTJl"; + String visitorData = "CgtFQThPOThGYzV0OCjDkqm3BjIKCgJVUxIEGgAgGg%3D%3D"; + RetrofitManager.getInstance().getHomeMoreData(continuation,clickTrackingParams,visitorData); + } + }); + } @Override diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c66375e..ae9e311 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,10 @@ - - \ No newline at end of file + + + + + \ No newline at end of file