【发布时间】:2018-12-06 13:06:45
【问题描述】:
我想将此程序转换为动态程序。从服务器获取 JSON 并返回到列表项。
public List<Item> getData() {
return Arrays.asList(
new Item(1, "Everyday" , "$12.00 USD"),
new Item(2, "Small Porcelain Bowl", "$50.00 USD"),
new Item(3, "Favourite Board", "$265.00 USD"),
);
}
我解析了 json 但返回类型错误。
Error:(73, 8) error: incompatible types: List<String> cannot be converted to List<Item>
错误:任务 ':app:compileDebugJavaWithJavac' 执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。 信息:在 13 秒内构建失败 信息:2个错误
这是我的代码:
List<String> listItems = new ArrayList<String>();
try {
URL arc= new URL(
"https://arc.000webhostapp.com/data/Cse.json");
URLConnection tc = arc.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
listItems.add(jo.getString("text"));
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listItem;
【问题讨论】:
-
对不起,我忘记添加我的完整代码
-
没关系,向我们展示您尝试过的方法,告诉我们哪些方法不起作用,我会投票并看看。
-
现在请检查代码。
标签: android listview arraylist