android - 执行 JSONArray JSONObject 后调试停止,无法执行 for 循环?
问题描述
我是新来的api
,我刚刚尝试了一个简单的api
调用示例http
并尝试将其放入ListView
,但它正在显示NULLPointerExecption
,我调试代码并在它到达JSONArray
对象创建后停止调试?为什么我真的不知道,我也尝试在gradle
文件中设置 Shinking 选项 true/false 但什么也没发生......
我只将我的整个项目包含在一个项目中mainactivity
。这是代码...
JSONArray ja = new JSONArray(data);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
singleParsed = "\n" + "\n" + "NAME : " + jo.get("name") + "\n" +
"PASSWORD : " + jo.get("password") + "\n" +
"CONTACT : " + jo.get("contact") + "\n" +
"COUNTRY : " + jo.get("country") + "\n";
dataParsed = dataParsed + singleParsed + "\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
try {
return ParseMoviesJsonString(dataParsed);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private ArrayList<User> ParseMoviesJsonString(String dataParsed) throws JSONException {
final String json_name = "name";
final String json_password = "password";
final String json_contact = "contact";
final String json_country = "country";
ArrayList<User> list = new ArrayList<>();
JSONObject MovieJson = new JSONObject(dataParsed);//MovieJson got the data and then skipps the followinglines and directly go to onPostExecute
JSONArray MovieArray = MovieJson.getJSONArray(dataParsed);
for (int i = 0; i < MovieArray.length(); i++) {
JSONObject result_movie = MovieArray.getJSONObject(i);
User movie = new User();
movie.setU_name(result_movie.getString(json_name));
movie.setU_password(result_movie.getString(json_password));
movie.setU_contact(result_movie.getString(json_contact));
movie.setU_country(result_movie.getString(json_country));
list.add(movie);
}
return list;
}
@Override
protected void onPostExecute(ArrayList<User> list) {// list : null
super.onPostExecute(list);
adapter.updateList(list);
adapter.notifyDataSetChanged();
}
}
}
解决方案
只需按照下面给出的编辑您的异步任务
@Override
protected ArrayList<User> doInBackground(String[] params) {
try {
URL url = new URL("https://api.myjson.com/bins/1422e4");
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
InputStream is = hc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while (line != null) {
line = br.readLine();
data = data + line;
}
/* JSONArray ja = new JSONArray(data);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
singleParsed = "\n" + "\n" + "NAME : " + jo.get("name") + "\n" +
"PASSWORD : " + jo.get("password") + "\n" +
"CONTACT : " + jo.get("contact") + "\n" +
"COUNTRY : " + jo.get("country") + "\n";
dataParsed = dataParsed + singleParsed + "\n";
}*/
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
return ParseMoviesJsonString(data);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private ArrayList<User> ParseMoviesJsonString(String dataParsed) throws JSONException {
final String json_name = "name";
final String json_password = "password";
final String json_contact = "contact";
final String json_country = "country";
ArrayList<User> list = new ArrayList<>();
JSONArray MovieArray = new JSONArray(dataParsed);
for (int i = 0; i < MovieArray.length(); i++) {
JSONObject result_movie = MovieArray.getJSONObject(i);
User movie = new User();
movie.setU_name(result_movie.getString(json_name));
movie.setU_password(result_movie.getString(json_password));
movie.setU_contact(result_movie.getString(json_contact));
movie.setU_country(result_movie.getString(json_country));
list.add(movie);
}
return list;
}
根据上述解决方案,您必须在 doinbackground 中注释代码,并在解析时进行一些更改。您也可以尝试使用 GSON 库,这是更好的方法。
推荐阅读
- php - 如何使用php获取访问者的IP地址
- git - 试图运行 cmake .. 命令
- c++ - 与 glfw 链接时出现问题
- google-apps-script - 如何像一组一样合并两个谷歌表格?
- android - 如何通过比较两个字段 Firestore 来获取文档
- python - 从图像中删除直线
- asp.net-core - 将 ASP.NET Core 3.1 应用部署到 Azure 应用服务后出现错误 500.31
- docker - Traefik 2.2 + Docker + Websocket (ws) 不工作
- webgl - 在 TWGL 中,为什么“setBuffersAndAttributes”和“drawBufferInfo”是两个单独的调用?
- python - .kv 文件中的错误==> 文件“/home/pi/Desktop/ali/my9.kv”