无法将 json 值传递给函数
cannot pass the json value to function
我将从本地主机数据库中获取 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"
您正在将字符串附加到 StringBuilder sb = new StringBuilder();
。你必须像这样调用 parseJSON(sb.toString());
因为 String json
只是一个指针不包含你想要的实际字符串。
我建议使用适当的 http 库来处理像 Volley 或 Retrofit 这样的请求...JSON 错误处理也是内置的,因此可以为此目的完全删除 AsyncTask
但是你有的很好,只有 json
不应该在 while 循环之后使用,它只是 http 响应的最后一行,而不是完整的 json (假设有多个行)
您真的应该考虑在 onPostExecute
中解析结果,甚至可能让解析方法 return 成为一个实际对象,或者显示到 UI
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();
您可以改为使用我的代码片段,它对我来说工作正常。现在你可以为你的 private void parseJSON(String json) 函数使用 son 变量。
我将从本地主机数据库中获取 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"
您正在将字符串附加到 StringBuilder sb = new StringBuilder();
。你必须像这样调用 parseJSON(sb.toString());
因为 String json
只是一个指针不包含你想要的实际字符串。
我建议使用适当的 http 库来处理像 Volley 或 Retrofit 这样的请求...JSON 错误处理也是内置的,因此可以为此目的完全删除 AsyncTask
但是你有的很好,只有 json
不应该在 while 循环之后使用,它只是 http 响应的最后一行,而不是完整的 json (假设有多个行)
您真的应该考虑在 onPostExecute
中解析结果,甚至可能让解析方法 return 成为一个实际对象,或者显示到 UI
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();
您可以改为使用我的代码片段,它对我来说工作正常。现在你可以为你的 private void parseJSON(String json) 函数使用 son 变量。