为什么我无法使用 android 中的改造从 API 获取body。它是返回代码 500,它在错误正文中返回我的所有数据



我不知道为什么我在错误正文中收到了所有数据,但我想在正文中接收数据。 如果是PHP,我是新手,请帮助我,这对我来说非常重要。 它是来自后端的返回代码 500 您可以查看我的日志 我在此处添加所有详细信息。

邮差

[
{
"id": "15",
"pathToFile": "1642584690_1590649486.mp4",
"categoryId": "9"
},
{
"id": "16",
"pathToFile": "1546153059_1590651415.mp4",
"categoryId": "9"
},
{
"id": "17",
"pathToFile": "https:/mozeloapp.in/viddo/UploadVideo/1590662957575.mp4",
"categoryId": "9"
}
]

php 脚本

<?php
$getCategoryId=$_GET['categoryId'];
$connection = mysqli_connect("localhost","u781384687_sudhakarsinu","Prabhat@01","u781384687_vdimazaapp");
if($getCategoryId){
$sql = "SELECT *from all_video where categoryId='$getCategoryId'";
}else{
$sql = "SELECT *from all_video";
}

$result = mysqli_query($connection,$sql);
$json_array = array();
while ($row = mysqli_fetch_assoc($result)) {
//  $json_array[] = $row;
array_push($json_array,
array(
"id"=> $row['id'],
"pathToFile"=> $row['pathToFile'],
"categoryId" => $row['categoryId']));
}
echo  json_encode($json_array);
mysqli.close($connection);
?>

波乔类

package com.example.retrofitapi;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class VideoModel {
@SerializedName("id")
@Expose
private String id;
@SerializedName("pathToFile")
@Expose
private String pathToFile;
@SerializedName("categoryId")
@Expose
private String categoryId;
public VideoModel() {
}
public VideoModel(String id, String pathToFile, String categoryId) {
this.id = id;
this.pathToFile = pathToFile;
this.categoryId = categoryId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPathToFile() {
return pathToFile;
}
public void setPathToFile(String pathToFile) {
this.pathToFile = pathToFile;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
}

接口

接口
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
public interface VideoApi {
String BASE_URL = "https://mozeloapp.in/";
String FEED_URL = "viddo/retrivevideo.php";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
@GET(FEED_URL)
Call<List<VideoModel>>getVideo();
}

玛尼活动

package com.example.retrofitapi;
import androidx.annotation.LongDef;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
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.getVideo();
call.enqueue(new Callback<List<VideoModel>>() {
@Override
public void onResponse(Call<List<VideoModel>> call, Response<List<VideoModel>> response) {
try {
Log.d("VIDEOLINK", "onResponse: error code"+response.errorBody().string());
Log.d("VIDEOLINK", "onResponse: code "+response.code());
Log.d("VIDEOLINK", "onResponse: body "+response.body());
} catch (IOException e) {
e.printStackTrace();
}

}
@Override
public void onFailure(Call<List<VideoModel>> call, Throwable t) {
}
});
}
}

日志猫

2020-05-28 17:24:07.772 12869-12869/com.example.retrofitapi D/VIDEOLINK: onResponse: error code[{"id":"15","pathToFile":"1642584690_1590649486.mp4","categoryId":"9"},{"id":"16","pathToFile":"1546153059_1590651415.mp4","categoryId":"9"},{"id":"17","pathToFile":"https:/mozeloapp.in/viddo/UploadVideo/1590662957575.mp4","categoryId":"9"}]
2020-05-28 17:24:07.772 12869-12869/com.example.retrofitapi D/VIDEOLINK: onResponse: code 500
2020-05-28 17:24:07.772 12869-12869/com.example.retrofitapi D/VIDEOLINK: onResponse: body null

每当你得到 500 个代码时,这意味着错误来自你的后端服务器,所以需要在 android 后端之间检查你的 API 东西。