Retrofit2与Query参数不起作用

时间:2016-11-22 08:26:43

标签: android json retrofit2

我有一些json对一些书的回应。

[
 {
"title" : "Clean Code",
"author" : "Robert Martin",
"bookUrl" : "http://amzn.to/1DJybxH",
"imageUrl" : "http://adavis.github.io/adept-android/images/clean_code.jpg",
"displayDate" : "August 11, 2008",
"numberOfPages" : 464
},

{
"title" : "Effective Java",
"author" : "Joshua Bloch",
"bookUrl" : "http://amzn.to/1Ku8Xel",
"imageUrl" : "http://adavis.github.io/adept-  
 android/images/effective_java.jpg",
"displayDate" : "May 28, 2008",
"numberOfPages" : 346
},

{
"title" : "Working Effectively with Legacy Code",
"author" : "Michael Feathers",
"bookUrl" : "http://amzn.to/1Jqe1PA",
"imageUrl" : "http://adavis.github.io/adept-android/images/legacy_code.jpg",
"displayDate" : "October 2, 2004",
"numberOfPages" : 456
},

{
"title" : "Refactoring: Improving the Design of Existing Code",
"author" : "Martin Fowler",
"bookUrl" : "http://amzn.to/1Lx4cjR",
"imageUrl" : "http://adavis.github.io/adept-android/images/refactoring.jpg",
"displayDate" : "July 8, 1999",
"numberOfPages" : 464
},

{
"title" : "Test Driven Development: By Example",
"author" : "Kent Beck",
"imageUrl" : "http://adavis.github.io/adept-android/images/tdd.jpg",
"displayDate" : "November 18, 2002",
"numberOfPages" : 240
}
]

现在我想执行一个搜索查询并仅显示标题中包含单词Code的对象,即有效地使用旧版代码等等。所以这就是我在做的事情。

public interface BookService {

  @GET("photos/sample_data")
  Call<List<Book>> getBooks();

  @GET("photos/sample_data")
  Call<List<Book>> search( @Query( "q" ) String query );

}

第一个@GET请求读取所有json数据。这部分内容正在发挥作用。现在第二个@GET请求引用了我的搜索方法。如您所见,它包含一个查询参数。

接下来,我在Presesenter类中调用enqueue(...)方法(我使用MVP)。所以

public class BooksPresenter {
private final BooksContract.View booksView;
private final BookService service;

public BooksPresenter(BooksContract.View booksView,BookService service){
    this.booksView = booksView;
    this.service = service;
}

public void initDataSet(){
    service.search("Code").enqueue(new Callback<List<Book>>() {
        @Override
        public void onResponse(Call<List<Book>> call, Response<List<Book>> response) {
            if(response.isSuccessful()){
                booksView.showBooks(response.body());
                Timber.i( "Books data was loaded from API." );

            }
        }

        @Override
        public void onFailure(Call<List<Book>> call, Throwable t) {
            booksView.showErrorMessage();
            Timber.e( t, "Unable to load the books data from API." );
        }
    });
   }
}

此行搜索单词代码。

service.search("Code")...

然而,我仍然得到了整本书,但查询不起作用。 :(。有什么想法吗?

我的整个网址是

http://www.theo-android.co.uk/photos/sample_data

我在一个单独的类中声明它的第一位。

public class Constants {
  public static final String BASE_URL = "http://www.theo-android.co.uk/";
}

谢谢,

西奥。

更新

我决定创建一个数据库并静态添加数据!接下来我写了一个php脚本来生成json。

<?php 
 include("init.php");
 $string="";
 $newString="";
 $get_posts = "select * from books_table";

$run_posts = mysqli_query($con,$get_posts); 

 $posts_array = array();

 while ($posts_row = mysqli_fetch_array($run_posts)){


               $row_array['title'] = $posts_row['title'];
               $row_array['author'] = $posts_row['author'];
               $row_array['bookUrl'] = $posts_row['bookUrl'];
               $row_array['imageUrl'] = $posts_row['imageUrl'];
               $row_array['displayDate'] = $posts_row['displayDate'];
               $row_array['numberOfPages'] = $posts_row['numberOfPages'];

               array_push($posts_array,$row_array);

 }

   $string = json_encode($posts_array,JSON_UNESCAPED_UNICODE);

   echo $string;
 ?>

我将网址更改为

http://www.theo-android.co.uk/books/sample_data.php

我让json回来,正如在开始时所示。但是有了这个网址

 www.theo-android.co.uk/books/sample_data.php/q=clean

仍有同样的问题。所以我想测试我的脚本,并添加where子句。

<?php 
 include("init.php");
$string="";
$newString="";
$get_posts = "select * from books_table where title='Clean Code' ";

$run_posts = mysqli_query($con,$get_posts); 

 $posts_array = array();

 while ($posts_row = mysqli_fetch_array($run_posts)){


               $row_array['title'] = $posts_row['title'];
               $row_array['author'] = $posts_row['author'];
               $row_array['bookUrl'] = $posts_row['bookUrl'];
               $row_array['imageUrl'] = $posts_row['imageUrl'];
               $row_array['displayDate'] = $posts_row['displayDate'];
               $row_array['numberOfPages'] = $posts_row['numberOfPages'];

               array_push($posts_array,$row_array);

  }

   $string = json_encode($posts_array,JSON_UNESCAPED_UNICODE);

   echo $string;
 ?>

猜猜是什么。我仍然得到整个json的回应!

2 个答案:

答案 0 :(得分:1)

试试这个

@GET("books/sample/")
  Call<List<Book>> search( @Query( "q" ) String query );
而不是这个

@GET("photos/sample_data")
  Call<List<Book>> search( @Query( "q" ) String query );

注意: - 您在评论中提及此网址与您在@Get方法中的使用不同... dropwizard-configurable-assets-bundle

答案 1 :(得分:0)

您可以在initDataSet中尝试使用此代码进行请求。

List<Book> bookList = service.search("Code");

也许这段代码可以帮到你。