【发布时间】: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 检查它是否工作......