【问题标题】:cannot pass the json value to function无法将 json 值传递给函数
【发布时间】:2019-11-25 20:34:49
【问题描述】:

我将从 localhost 数据库获取 JSON。然后,我想把 json 放到 ArrayList 中。我想调用 parseJSON 函数。但 JSON 为空。我应该在哪里调用该函数,非常感谢:))

        @Override
        protected String doInBackground(Void... voids) {
                    try {
                        URL url = new URL(urlWebService);
                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        con.setRequestMethod("GET");
                        StringBuilder sb = new StringBuilder();
                        InputStream input = con.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
                        String json;
                        while ((json = bufferedReader.readLine()) != null) {
                            sb.append(json + "\n");
                        }
                        parseJSON(json);
                        return sb.toString().trim();
                    } catch (Exception e) {
                        return null;
            }
        }
    }
    GetJSON getJSON = new GetJSON(
    );
    getJSON.execute();
}

private void parseJSON(String json) {
    Gson gson = new Gson();
    Type type = new TypeToken<List<EssayElement>>(){}.getType();
    List<EssayElement> mList = gson.fromJson(json, type);
    for (EssayElement essayElement : mList){
    Log.i("Message: " +
            "", essayElement.id + "-" + essayElement.title + "-" + essayElement.essay + "-" + essayElement.date + "-" + essayElement.time);
    }
}

使用字符串“json”的空对象引用

【问题讨论】:

  • 尝试用parseJSON(sb.toString());替换parseJSON(json);
  • 缺少json=sb.toString()。json变量仍然没有引用。
  • 在 parseJSON 函数中,您发送的是空值,因为您没有为“json”设置值,请尝试“发送 sb.toString()”并更改参数

标签: java android json android-asynctask


【解决方案1】:

我建议使用适当的 http 库来处理像 Volley 或 Retrofit 这样的请求... JSON 和错误处理也是内置的,因此可以完全删除 AsyncTask

但是你所拥有的很好,只有json不应该在while循环之后使用,它只是http响应的最后一行,而不是完整的json(假设有多行)

您真的应该考虑解析onPostExecute 中的结果,甚至可能让 parse 方法返回一个实际对象,或显示到 UI

【讨论】:

  • library that handles parsing for you like Volley or Retrofit VolleyRetrofit 都与 parsing 无关
  • 当然,JsonObjectRequest 可以在内部使用 Gson/Jackson/Moshi
【解决方案2】:

您将字符串附加到StringBuilder sb = new StringBuilder();。你必须像这样调用parseJSON(sb.toString()); 因为String json 只是一个指针,不包含实际的字符串,你想要。

【讨论】:

    【解决方案3】:
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            String json = sb.toString();
    

    您可以改用我的代码 sn-p 它对我来说工作正常。现在您可以将儿子变量用于您的私有 void parseJSON(String json) 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2017-01-18
      相关资源
      最近更新 更多