首页接口解析

This commit is contained in:
litingting 2024-09-18 15:08:32 +08:00
parent 9a3baf1261
commit 0b7ec644cd
13 changed files with 469 additions and 111 deletions

View File

@ -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<T> {
void onFail(String errorMsg);

View File

@ -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;
}
}
}

View File

@ -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;
// }
// }
}

View File

@ -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;
}
}

View File

@ -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<ResponseHomeChild> childList;
public List<ResponseHomeChild> getChildList() {
return childList;
}
public void setChildList(List<ResponseHomeChild> 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;
}
}

View File

@ -0,0 +1,36 @@
package com.hi.music.player.javabean.response;
import java.util.List;
public class ResponseHomeChild {
private String HeaderTitle;
private List<ResponseSingle> singleList;
private List<ResponseCategory> categoryList;
public String getHeaderTitle() {
return HeaderTitle;
}
public void setHeaderTitle(String headerTitle) {
HeaderTitle = headerTitle;
}
public List<ResponseSingle> getSingleList() {
return singleList;
}
public void setSingleList(List<ResponseSingle> singleList) {
this.singleList = singleList;
}
public List<ResponseCategory> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<ResponseCategory> categoryList) {
this.categoryList = categoryList;
}
}

View File

@ -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;
}
}

View File

@ -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<ResponseHomeChild> 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<ResponseCategory> categoryList = new ArrayList<>();
List<ResponseSingle> 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 "";
}
}
}

View File

@ -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<ResponseBody> 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<ResponseBody> 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<ResponseBody> getHomeMoreData(@FieldMap Map<String,String> map);
}

View File

@ -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;

View File

@ -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<String, String> 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<ResponseBody>(new RequestListener<ResponseBody>() {
@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("");
}
}
}));
}
}

View File

@ -9,7 +9,7 @@ import com.hi.music.player.ui.BaseActivity;
public class MainActivity extends BaseActivity<ActivityMainBinding> {
// EdgeToEdge.enable(this);
// EdgeToEdge.enable(this);
@Override
protected ActivityMainBinding getViewBinding() {
return ActivityMainBinding.inflate(getLayoutInflater());
@ -20,10 +20,21 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
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

View File

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.MainActivity">
<TextView
@ -16,4 +17,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tv1"
android:layout_marginTop="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>