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

Viewed 53

Failed to receive body from response. i used php as back end server in android using 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.

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

import 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

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());

            }
        });

    }
}
1 Answers

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

"onResponse: null"

Correct your SerializedName definitions:

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

Related