私はいくつかの書籍で次のようなjson応答を持っています。Retrofit2 with Queryパラメータが機能しません
[
{
"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
}
]
は、今私はさがすクエリをperforme、そのタイトルレガシーコードとし、その1効果的に動作するコードすなわち言葉が含まれているオブジェクトのみを表示したいです。これは私がやっていることです。
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データを読み出します。その部分が働いているように。 2番目の@GET要求は私の検索方法を参照しています。ご覧のとおり、クエリパラメータが含まれています。
次は私のPresesenterクラス(私はMVPを使用しています)でenqueue(...)メソッドを呼び出します。したがって
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")...
しかし、私はまだ全書籍を手に入れており、クエリは機能しません。 :(。任意のアイデア?
私の全体のURLは
http://www.theo-android.co.uk/photos/sample_data
であると私は別のクラスでの最初のビットを宣言。
public class Constants {
public static final String BASE_URL = "http://www.theo-android.co.uk/";
}
おかげで、
テオ。
更新
私はデータベースを作成し、データを静的に追加することに決めました!次に、jsonを生成するためのPHPスクリプトを作成しました。
<?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;
?>
私はbegginingで示したように私は、JSONを取り戻す
http://www.theo-android.co.uk/books/sample_data.php
にURLを変更しました。しかし、このURLで
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応答を得る!
好奇心の理由、なぜそれが本を返すときにエンドポイント '写真'を指定したのですか?私はブラウザからエンドポイントを照会しようとしましたが、私は同じ結果を得ています。間違ったエンドポイントを照会しているか、バックエンドが機能しません。 – Blackbelt
@Blackbeltそれは私のサーバーの古いフォルダでした。私はそれを変更します。しかし、このURL theo-android.co.uk/photos/sample_data/q=codeはまだ全体のjsonを示しています。 – Theo
問題はバックエンド側です。これは改造されていません – Blackbelt