retrofit2中关于baseUrl问题(一)

2017-07-05  本文已影响0人  墨色尘埃

http://api.m.mtime.cn/PageSubArea/TrailerList.api
两种解析方法,一种失败还有一种成功。

组合一://失败
baseUrl("http://api.m.mtime.cn/PageSubArea/TrailerList.api/")
@GET("/")
Observable<DoubanBook> getDouban(); 

组合二://成功
baseUrl("http://api.m.mtime.cn/")
@GET("PageSubArea/{address}") 
Observable<DoubanBook> getDouban1(@Path("address")String address); 

组合三://成功
baseUrl("http://api.m.mtime.cn/")
@GET("PageSubArea/TrailerList.api")
Observable<DoubanBook> getDouban2();  

组合四://成功
baseUrl("http://api.m.mtime.cn/PageSubArea/")
@GET("TrailerList.api")
Observable<DoubanBook> getDouban3();  

组合五://成功
baseUrl("http://api.m.mtime.cn/PageSubArea/")
@GET("{address}")
Observable<DoubanBook> getDouban4(@Path("address")String address); 

失败,还没搞懂什么原因,可能是baseUrl里的部分是全的吧?

public void getDoubanBook() { //失败,可能是解析json出问题了?? response为空  HTTP 403 Forbidden
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.m.mtime.cn/PageSubArea/TrailerList.api/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        BaiDuService service = retrofit.create(BaiDuService.class);
        service.getDouban()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( new Consumer<DoubanBook>() {
                    @Override
                    public void accept(@NonNull DoubanBook doubaiRoot) throws Exception {
                        text.setText(doubaiRoot.getTrailers().get(0).getSummary().toString());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        Log.e("MainActivity", throwable.getMessage());
                    }
                });

//        call.enqueue(new Callback<DoubaiRoot>() {
//            @Override
//            public void onResponse(Call<DoubaiRoot> call, Response<DoubaiRoot> response) {
//                Toast.makeText(MainActivity.this, "获取成功"+response.body(), Toast.LENGTH_SHORT).show();
////                text.setText(response.body().toString());
//            }
//
//            @Override
//            public void onFailure(Call<DoubaiRoot> call, Throwable t) {
//                Log.e("MainActivity", t.getMessage());
//            }
//        });
    }

成功

// TODO: 2017/7/5 终于解决这个问题了,gsonformat生成的实体类中 rating参数有误,改为double型即可
    public void getDoubanBook1() { //失败 -->成功
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.m.mtime.cn/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        BaiDuService service = retrofit.create(BaiDuService.class);
        service
                .getDouban2()                 //成功
//                .getDouban1("TrailerList.api")  //成功
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<DoubanBook>() {
                    @Override
                    public void accept(@NonNull DoubanBook doubanBook) throws Exception {
                        Toast.makeText(MainActivity.this, "获取成功" + doubanBook.getTrailers(), Toast.LENGTH_SHORT).show();
                        text.setText(doubanBook.getTrailers().get(0).getSummary());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        Log.e("MainActivity", throwable.getMessage());
                    }
                });
    }

BaiDuService

    @GET("/") //如果这里参数,则最前面要加斜杠
    Observable<DoubanBook> getDouban(); //失败

    @GET("PageSubArea/{address}")
    Observable<DoubanBook> getDouban1(@Path("address")String address); //成功

    @GET("PageSubArea/TrailerList.api")
    Observable<DoubanBook> getDouban2();  //成功

    @GET("TrailerList.api")
    Observable<DoubanBook> getDouban3();  //成功

//    @GET("/{address}") //如果这里参数,则最前面不要加斜杠
    @GET("{address}")
    Observable<DoubanBook> getDouban4(@Path("address")String address);  //成功
上一篇下一篇

猜你喜欢

热点阅读