我正在使用Android上的 kotlin 应用程序,并且正在使用Fuel从REST API中获取数据。
我想用我的API中的数据填充 ArrayList 。
但是,如果我调试代码,进入 Fuel 部分后,我的ArrayList仍然为空(但如果在Fuel内部打印,则为空)。
为什么?我如何在Fuel部分之外得到结果?
val receivedAlerts = ArrayList<String>()
var jsonArray : JSONArray
val resultActus = Fuel.get(Constants.urlAlertes).responseJson { request, response, result ->
Log.i(TAG, result.get().obj().toString())
result.fold({
json ->
jsonArray = JSONArray(json.content)
if(jsonArray.length() > 0){
(0 until jsonArray.length()).forEach {
val item = jsonArray.getJSONObject(it)
val id = item.getString("ID")
receivedAlerts.add(id)
}
}
}, {
err ->
Log.i("error", err.toString())
})
}
Log.i(TAG, "from getReceivedAlerts $receivedAlerts")
没人知道吗?
答案 0 :(得分:1)
燃料正在后台线程上工作,这意味着该函数在填充数组之前返回。
示例:
函数调用:
fun sendToAPI(context: Context, data: someData, f: () -> Unit)
如您所见,在函数调用中,我们将函数作为参数(f)传递,这将在以后有用。
SomeURL.httpPost(formData)
.response { _, _, result -> result.fold(
success = {
// Save data in the array
// Runs function
f()
context.showToast("success")
},
failure = { error ->
context.showToast(error.message!!)
}
) }
基本上,在成功语句中您要保存数组,可以用不同的方法来保存,我个人希望有一个单例存储我的所有数据(您可以在kotlin中用object关键字创建一个)>
然后,我们运行作为参数传递的函数,在该函数中,您要从单例中获取数据并执行所需的操作。
答案 1 :(得分:0)
默认情况下,Fuel异步进行呼叫。我个人喜欢在应用程序的不同部分中同步使用燃料和处理异步。
<table class="table table-hover">
<!-- table table-striped table-bordered table-hover begin -->
<thead>
<!-- thead begin -->
<tr>
<!-- tr begin -->
<th scope="col"> ISBN: </th>
<th scope="col"> Book Title: </th>
<th scope="col"> Publisher: </th>
<th scope="col"> Publication Year: </th>
<th scope="col"> Category: </th>
<th scope="col"> Number of copies: </th>
<th scope="col"> Edit: </th>
<th scope="col"> Delete: </th>
</tr><!-- tr finish -->
</thead><!-- thead finish -->
<tbody>
<!-- tbody begin -->
<?php
$i = 0;
$get_books = "SELECT book.ISBN, book.bookTitle, book.bookPublisher, book.bookPublicationDate, bookcategory.categoryName FROM book INNER JOIN bookcategory ON book.bookCategory = bookcategory.categoryID ";
// $get_payments = "SELECT * FROM books";
$run_books = mysqli_query($conn, $get_books);
while ($book_row = mysqli_fetch_array($run_books)) {
$ISBN = $book_row['ISBN'];
$title = $book_row['bookTitle'];
$publisher = $book_row['bookPublisher'];
$pubdate = $book_row['bookPublicationDate'];
$cat_name = $book_row['categoryName'];
// get total number of copies for each book
$count_copies = "SELECT * FROM bookcopy WHERE ISBN = $ISBN ";
$run_count = mysqli_query($conn, $count_copies);
$copy_num = mysqli_num_rows($run_count);
// get all authors
$authors_array = array();
$get_authors = "SELECT author.authorsFullName FROM book INNER JOIN book_has_authors ON book_has_authors.book_ISBN = book.ISBN INNER JOIN author ON author.authorID = book_has_authors.authors_authorID WHERE ISBN=$ISBN;";
$run_authors = mysqli_query($conn, $get_authors);
while($author_row = mysqli_fetch_array($run_authors)){
$authors_array[] = $author_row;
}
$i++;
echo '<tr scope="row"><!-- tr begin -->
<td>' . $ISBN . '</td>
<td>' . $title . '</td>
<td>' . $publisher . '</td>
<td>' . $pubdate . '</td>
<td>' . $cat_name . '</td>
<td>' . $copy_num. '</td>
<td><a href="index.php?delete_product' . $ISBN . '>
<i class="fas fa-trash"></i> Delete
</a>
</td>
<td>
<a href="index.php?edit_product' . $ISBN . '>
<i class="fa fa-pencil"></i> Edit
</a>
</td>
</tr><!-- tr finish -->
';
}
?>
</tbody><!-- tbody finish -->
</table>