Android - 如何使用原始PHP使用Retrofit获取数据?

时间:2017-08-07 18:11:53

标签: java php android json retrofit

我想使用原始PHP从Web服务API获取对象数据,但我无法得到它。它总是向我显示失败块回调。 这是我的网址。

  

http://localhost/khmersongapi/allsong.php

服务生成器

public class ServiceGenerator {

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    private static Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://10.0.3.2/khmersongapi/")
            .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass) {
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                okhttp3.Request original = chain.request();
                Request.Builder requestBuilder = original.newBuilder()
                        .header("Accept", "application/json");
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });


        OkHttpClient client = httpClient.build();
        Retrofit retrofit = builder.client(client).build();
        return retrofit.create(serviceClass);
    }

}

回复正文

public class SongRespone {


    @SerializedName("id")
    private String id;

    @SerializedName("song_name")
    private String songName;

    @SerializedName("song_url")
    private String songUrl;

    @SerializedName("singer_name")
    private String singerName;

    @SerializedName("singer_image")
    private String singerImage;

    @SerializedName("type_name")
    private String typeName;

    @SerializedName("category_name")
    private String categoryName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSongName() {
        return songName;
    }

    public void setSongName(String songName) {
        this.songName = songName;
    }

    public String getSongUrl() {
        return songUrl;
    }

    public void setSongUrl(String songUrl) {
        this.songUrl = songUrl;
    }

    public String getSingerName() {
        return singerName;
    }

    public void setSingerName(String singerName) {
        this.singerName = singerName;
    }

    public String getSingerImage() {
        return singerImage;
    }

    public void setSingerImage(String singerImage) {
        this.singerImage = singerImage;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    @Override
    public String toString() {
        return "SongRespone{" +
                "id='" + id + '\'' +
                ", songName='" + songName + '\'' +
                ", songUrl='" + songUrl + '\'' +
                ", singerName='" + singerName + '\'' +
                ", singerImage='" + singerImage + '\'' +
                ", typeName='" + typeName + '\'' +
                ", categoryName='" + categoryName + '\'' +
                '}';
    }
}

界面服务

public interface SongService {

    @GET("allsong.php")
    Call<SongRespone> findAllSong();

}

我尝试回应它,但它无法取得成功。

SongService songService = ServiceGenerator.createService(SongService.class);
        Call<SongRespone> call = songService.findAllSong();
        call.enqueue(new Callback<SongRespone>() {
            @Override
            public void onResponse(Call<SongRespone> call, Response<SongRespone> response) {
                Log.e("pppppp", response.body().toString());
            }

            @Override
            public void onFailure(Call<SongRespone> call, Throwable t) {
                Log.e("pppppp","onFailure");
                t.printStackTrace();
            }
        });

它显示我在失败的阻止。 如何显示JSON对象?请帮帮我

0 个答案:

没有答案