【问题标题】:Retrofit2 with Query parameter that doesn't work带有不起作用的查询参数的 Retrofit2
【发布时间】:2016-11-22 18:22:47
【问题描述】:

我有一些书籍的以下 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 的对象,即有效地使用 Legacy 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 响应!!!

【问题讨论】:

  • 出于好奇,为什么在还书时将端点命名为photos?我试图从浏览器查询您的端点,我得到了相同的结果。要么您查询了错误的端点,要么您的后端不工作
  • @Blackbelt 这是我服务器中的一个旧文件夹。我会改变它。但是这个 url theo-android.co.uk/photos/sample_data/q=code 仍然显示整个 json。
  • 问题出在后端。不是改造
  • 所以这是你的后端的问题,而不是改造查询。
  • 用 POSTMAN 检查它是否工作......

标签: android json retrofit2


【解决方案1】:

试试这个

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

而不是这个

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

注意:-您在评论中提到此 URL 与您在 @Get 方法中使用的不同...http://www.theo-android.co.uk/books/sample/q=code

【讨论】:

  • 我已经尝试过了,但不起作用。我仍然得到所有 json 对象:(
  • 停止是什么意思?
  • 这意味着当URL命中服务器时服务器出现问题,它关闭了服务器并且服务器不再打开..... URL is NOT WORKING .. ..
  • 我明白了。也许我应该给服务器提供商打个电话
【解决方案2】:

你可以试试这个代码在 initDataSet 中的请求吗?

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

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

【讨论】:

    猜你喜欢
    • 2018-02-12
    • 2017-08-12
    • 2020-12-10
    • 2017-05-13
    • 2021-12-12
    • 2020-02-03
    • 1970-01-01
    • 2017-05-09
    • 2013-06-17
    相关资源
    最近更新 更多