简体   繁体   English

使用 Retrofit 和 PHP 作为后端,无法从 android 中的服务器接收任何响应

[英]Failed to receive any response from server in android using Retrofit and PHP as backend

Failed to receive body from response.无法从响应中接收正文。 i used php as back end server in android using Retrofit.我使用 php 作为 android 中的后端服务器,使用 Retrofit。 in my every project i used retrofit but this time i failed getting response body from server i don't know where is the problem.在我的每个项目中,我都使用了 retrofit 但这次我未能从服务器获取响应正文,我不知道问题出在哪里。

I search all over the internet but i failed to get out myself from the problem.我在整个互联网上搜索,但我未能从问题中解脱出来。 in below i put all of my code.在下面我把我所有的代码。 Thanks you a lot in advance非常感谢你提前

VideoApi.class VideoApi.class

import java.util.List;导入 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();

    }

ModelClass.java ModelClass.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 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());

            }
        });

    }
}

You seem to be wrong in the attributes you parse.您解析的属性似乎是错误的。 I expect that from this line:我希望从这一行:

Log.d("VIDEOLINK", "onResponse: "+link);

You get the output你得到 output

"onResponse: null"

Correct your SerializedName definitions:更正您的SerializedName定义:

@SerializedName("name")
@Expose
private String categoryName;
@SerializedName("link")
@Expose
private String videoLink;

I am basing my answer on what I see as response from your API.我的回答基于我从您的 API 看到的响应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android:如何使用 Retrofit 接收来自以下 json 的响应? - android: How to receive response from the following json using Retrofit? 无法从Android上的Telnet服务器收到任何响应 - Can't receive any response from Telnet server on Android 使用改造将图像从 android 上传到 PHP 服务器 - Image uploading from android to PHP server using retrofit 响应不是来自使用改造 2 的服务器 - Response is not coming from server using retrofit2 如何从Android向PHP服务器localhost发送请求(字符串数据)并从服务器PHP接收响应结果 - How to send request(String data) from android to PHP server localhost and receive response result form server PHP 从android中的php后端获取JSON响应 - Get JSON response from php backend in android 我无法在使用 android 中使用 java 的改造 2 中收到呼叫响应无法解析 model - I cannot receive response in call using retrofit2 using java in android cannot parse response in model 在 android 中调用 Api 并使用 Retrofit 2 收到此错误:Response{protocol=h2, code=200, message=, url=https://api.com/video/list-video.php} - Api call in android and receive this error using Retrofit 2 :Response{protocol=h2, code=200, message=, url=https://api.com/video/list-video.php} Android:如何使用改型从json响应中获取特定字符串? - Android: How to get specific string from json response using retrofit? 使用改造无法从android中的json响应中获取light_id - Not getting light_id from json response in android using retrofit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM