用Volley请求JSON

时间:2018-04-26 09:58:05

标签: android sqlite android-volley jsonobjectrequest

我试图创建一个显示存储在JSONObject中的文章的应用程序。 我在我的服务器上使用php函数来获取包含我的数据库的JSON文件。在我的Android应用程序中,我希望获得JSON并将其放在private void initDataset() { mDataset = new ArrayList<>(); Log.d("InitDataset", String.valueOf(mDataset.size())); getArticles(); Log.d("InitDataset", String.valueOf(mDataset.size())); } public void getResponse(int method, String url, JSONObject jsonValue, final VolleyCallback callback) { StringRequest strreq = new StringRequest(method, url, new Response.Listener < String > () { @Override public void onResponse(String Response) { callback.onSuccessResponse(Response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError e) { e.printStackTrace(); Toast.makeText(getContext(), e + "error", Toast.LENGTH_LONG).show(); } }); AppController.getInstance().addToRequestQueue(strreq); } public void getArticles() { getResponse(Request.Method.GET, AppConfig.URL_ARTICLE, null, new VolleyCallback() { @Override public void onSuccessResponse(String result) { for (int i = 1; i < 3; i++) { try { Article article = new Article(); JSONObject response = new JSONObject(result); // Now store the articles in SQLite JSONObject articleObj = response.getJSONObject("article" + i); article.setArticle_id(i); article.setPicture_url(articleObj.getString("picture_url")); article.setName(articleObj.getString("name")); article.setDescription(articleObj.getString("description")); article.setQuantity(Float.parseFloat(articleObj.getString("quantity"))); article.setPrice(Float.parseFloat(articleObj.getString("price"))); mDataset.add(article); } catch (JSONException e) { e.printStackTrace(); } } } }); } public interface VolleyCallback { void onSuccessResponse(String result); } 中,我执行了以下操作:

mDataset

但是在日志中,onResponse()的大小始终为0.或者如果我在<?php require_once 'include/DB_Functions.php'; $db = new DB_Functions(); // JSON response array $response = array("error" => FALSE); $article = $db->getAllArticles(); if ($article != false) { // use is found $response["error"] = FALSE; while($row = $article->fetch_assoc()) { $response["article".$row["article_id"]]["article_id"] = $row["article_id"]; $response["article".$row["article_id"]]["picture_url"] = $row["picture_url"]; $response["article".$row["article_id"]]["name"] = $row["name"]; $response["article".$row["article_id"]]["description"] = $row["description"]; $response["article".$row["article_id"]]["quantity"] = $row["quantity"]; $response["article".$row["article_id"]]["price"] = $row["price"]; } echo json_encode($response); $fp = fopen('results.json', 'w'); fwrite($fp, json_encode($response)); fclose($fp); } else { $response["error"] = TRUE; $response["error_msg"] = "Error"; echo json_encode($response); } ?> 中记录文章的名称,我可以看到每个名字都在数据库中。 (因此我认为连接和php功能都没问题)

有什么想法吗?

这是php文件:

{
  "error": false,
  "article1": {
    "article_id": "1",
    "picture_url": "https://static.webshopapp.com/shops/019852/files/024106649/600x600x2/brasserie-dachouffe-la-chouffe-33cl.jpg",
    "name": "Chouffe",
    "description": "Ceci est une description de la chouffe.",
    "quantity": "33",
    "price": "2.54"
  },
  "article2": {
    "article_id": "2",
    "picture_url": "https://www.latelierdesbieres.fr/1266-large_default/biere-belge-noel-n-ice-chouffe-33-cl.jpg",
    "name": "Chouffe de Noel",
    "description": "Ceci est une description de la chouffe de Noel.",
    "quantity": "33",
    "price": "3.23"
  }
}

执行php时我得到的JSON:

{{1}}

1 个答案:

答案 0 :(得分:0)

您误解了一些异步执行顺序。

您需要重写方法以等待返回的结果,而不是立即将列表大小记录两次而不让网络调用完成

getArticles(new VolleyCallback() {
    @Override
    public void onSuccessResponse(String result) {
        // parse result 
        // print list size <-- shouldn't be empty 
    } 
});
// your list will be empty here still

方法定义为

public void getArticles(VolleyCallback  vcb) {
    getResponse(Request.Method.GET, AppConfig.URL_ARTICLE, null, vcb);
}

然而,当您可以使用正确的参数直接调用getResponse时,包装两个方法似乎毫无意义