java - 使用 Retrofit 和 PHP 作为后端,无法从 android 中的服务器接收任何响应
问题描述
无法从响应中接收正文。我使用 php 作为 android 使用 Retrofit 的后端服务器。在我的每个项目中,我都使用了改造,但这次我未能从服务器获取响应正文,我不知道问题出在哪里。
我在整个互联网上搜索,但我未能从问题中解脱出来。在下面我把我所有的代码。非常感谢你提前
VideoApi.class
导入 java.util.List;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
public interface VideoApi {
String BASE_URL = "https://mozeloapp.in/ViddoApp/ViddoVideoRetrive.php/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
@GET(BASE_URL)
Call<List<VideoModel>> getVideoLink();
}
模型类.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class VideoModel {
@SerializedName("id")
@Expose
private String id;
@SerializedName("categoryId")
@Expose
private String categoryId;
@SerializedName("categoryName")
@Expose
private String categoryName;
@SerializedName("videoLink")
@Expose
private String videoLink;
public VideoModel() {
}
public VideoModel(String id, String categoryId, String categoryName, String videoLink) {
this.id = id;
this.categoryId = categoryId;
this.categoryName = categoryName;
this.videoLink = videoLink;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getVideoLink() {
return videoLink;
}
public void setVideoLink(String videoLink) {
this.videoLink = videoLink;
}
}
MainActivity.java
private VideoApi videoApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoApi = VideoApi.retrofit.create(VideoApi.class);
Call<List<VideoModel>> call = videoApi.getVideoLink();
call.enqueue(new Callback<List<VideoModel>>() {
@Override
public void onResponse(Call<List<VideoModel>> call, Response<List<VideoModel>> response) {
if (response.isSuccessful()){
for (VideoModel videoModel : response.body()){
String link = videoModel.getVideoLink();
Log.d("VIDEOLINK", "onResponse: "+link);
}
}else {
Log.d("VIDEOLINK", "onResponse: "+"failed");
}
}
@Override
public void onFailure(Call<List<VideoModel>> call, Throwable t) {
Log.d("VIDEOLINK", "onResponse: "+t.getLocalizedMessage());
}
});
}
}
解决方案
您解析的属性似乎是错误的。我希望从这一行:
Log.d("VIDEOLINK", "onResponse: "+link);
你得到输出
"onResponse: null"
更正您的SerializedName
定义:
@SerializedName("name")
@Expose
private String categoryName;
@SerializedName("link")
@Expose
private String videoLink;
我的回答基于我看到的来自您的 API 的响应。
推荐阅读
- javascript - 从 bootstrap-vue 导入 Pagination、FormSelect 的警告
- reactjs - 如何使用键盘快捷键打开 PWA?
- javascript - 如何在自己的类中使用 pdf.js 承诺?
- extjs - Sencha Extjs:自动布局容器不占用更多宽度来容纳垂直滚动条。它是一个错误吗?
- python - Python 获取第二个值,忽略第一个
- signalr - 我不断抛出异常:System.InvalidOperationException with signalr
- javascript - 如何使用选择/选项标签在表单元素中触发 HTML 中的 JS 函数?
- ruby-on-rails - 如何在 Rails 5.2 中使用命名空间控制器基于对象类路由操作?
- firebase - Flutter Firebase 在新请求时通知管理员
- bash - 如何提示问题,并在 sed 脚本中使用答案