gpt4 book ai didi

java - Android Retrofit API 使用

转载 作者:行者123 更新时间:2023-11-30 05:11:02 25 4
gpt4 key购买 nike

我仍然是 android 改造 API 的初学者,我试图将一个变量传递到我的动态 url,但它向它添加了额外的字符,这些字符是“&=”。

这是我尝试使用的端点的样子:

https://www.example.com/api/index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/9392

其中“9232”是我要传递的 ID。然而,当我使用改造库时,这是我生成的 Url 的样子:

https://www.example.com/api/index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/&=9392

注意 &= 附加到正在发送的 Id

// Method that receives id and calls the retrofit API
private void getPlaylist(String id) {
/*Create handle for the RetrofitInstance interface*/
GetPlaylistRetrofitInterface playlistService = PlaylistClientInstance.getRetrofitInstance().create(GetPlaylistRetrofitInterface.class);

Call<List<Playlists>> call = playlistService.getPlaylist(id);
Log.d("URL", "getPlaylist: " + call.request().url());
call.enqueue(new Callback<List<Playlists>>() {
@Override
public void onResponse(Call<List<Playlists>> call, Response<List<Playlists>> response) {
myProgressBar.setVisibility(View.GONE);
populatePlayList(response.body());
}

@Override
public void onFailure(Call<List<Playlists>> call, Throwable throwable) {
myProgressBar.setVisibility(View.GONE);
Log.d("onFailure", "onFailure: " + throwable);
Toast.makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_LONG).show();
}
});
}

这是我接收 url id 并将其附加到端点的接口(interface)

public interface GetPlaylistRetrofitInterface {
@GET("index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/")
Call<List<Playlists>> getPlaylist(@Query(value = "") String id);
}

我试过在我的界面中使用@Path

public interface GetPlaylistRetrofitInterface {
@GET("index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/{id}")
Call<List<Playlists>> getPlaylist(@Path("id") String id);
}

但是,它使我的应用程序因以下错误而崩溃:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demoapp.HomeActivity}:
java.lang.IllegalArgumentException: URL query string "/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/{id}" must not have replace block. For dynamic query parameters use @Query.
for method GetPlaylistRetrofitInterface.getPlaylist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

提前致谢

好的,所以我能够解决这个问题,结果我试图通过使用@Query 将查询传递给已经是查询的端点,所以这就是我修复它的方法:

这是我更新的 GetPlaylistInterface

public interface GetPlaylistRetrofitInterface {
@GET()
Call<List<Playlists>> getPlaylist(@Url String url);
}

这是我更新的获取播放列表方法

private void getPlaylist(String id) {
myProgressBar.setVisibility(View.VISIBLE);
/*Create handle for the RetrofitInstance interface*/
GetPlaylistRetrofitInterface playlistService = RetrofitClientInstance.getRetrofitInstance().create(GetPlaylistRetrofitInterface.class);
Call<List<Playlists>> call = playlistService.getPlaylist(Config.playlist_url+id);
Log.d("URL", "getPlaylist: " + call.request().url());
call.enqueue(new Callback<List<Playlists>>() {
@Override
public void onResponse(Call<List<Playlists>> call, Response<List<Playlists>> response) {
myProgressBar.setVisibility(View.GONE);
populatePlayList(response.body());
}

@Override
public void onFailure(Call<List<Playlists>> call, Throwable throwable) {
myProgressBar.setVisibility(View.GONE);
Log.d("onFailure", "onFailure: " + throwable);
Toast.makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_LONG).show();
}
});
}

这是包含端点的配置类:

public class Config {
public static String playlist_url = "index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/";
public static String playlist_details_url = "index.php?/Tracks/get/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/";
}

最佳答案

您使用的是@Query 参数,默认情况下它具有此语法。您需要的是使用 @Path 而不是 @Query。您还需要将参数名称包含到您的 url 字符串中。

您的代码看起来像这样:

public interface GetPlaylistRetrofitInterface {
@GET("index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/{id}")
Call<List<Playlists>> getPlaylist(@Path("id") String id);

关于java - Android Retrofit API 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com