Android 调试在执行JSONArray JSONObject后停止,是否无法执行for循环?
我在调用Android 调试在执行JSONArray JSONObject后停止,是否无法执行for循环?,android,arrays,json,for-loop,debugging,Android,Arrays,Json,For Loop,Debugging,我在调用api方面是新手,我刚刚尝试了一个简单的api调用示例,使用http并尝试将其放入ListView,但它显示了NULLPointerExecption,我调试代码并在到达JSONArray对象创建后停止调试?为什么我真的不知道,我也试着在gradle文件中设置Shinking选项true/false,但什么都没有发生 我只在一个main活动中包含了我的整个项目。这是代码 JSONArray ja = new JSONArray(data);
api
方面是新手,我刚刚尝试了一个简单的api
调用示例,使用http
并尝试将其放入ListView
,但它显示了NULLPointerExecption
,我调试代码并在到达JSONArray
对象创建后停止调试?为什么我真的不知道,我也试着在gradle
文件中设置Shinking选项true/false,但什么都没有发生
我只在一个main活动中包含了我的整个项目。这是代码
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();
}
}
}
JSONArray ja=新的JSONArray(数据);
对于(int i=0;i
只需根据下面给出的内容编辑您的任务
@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;
}
@覆盖
受保护的ArrayList doInBackground(字符串[]参数){
试一试{
URL=新URL(“https://api.myjson.com/bins/1422e4");
HttpURLConnection hc=(HttpURLConnection)url.openConnection();
InputStream is=hc.getInputStream();
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
while(行!=null){
line=br.readLine();
数据=数据+行;
}
/*JSONArray ja=新JSONArray(数据);
对于(int i=0;i
根据上述解决方案,您必须在doinbackground中注释代码,并在解析中进行一些更改。您也可以尝试使用GSON库,这是更好的方法。为什么要发布整个活动代码?只需发布相关代码和logcat。@您可以发布JASON响应和logcat errorAccess denied finding属性“hwservicemanager.ready”DisplayBase::BuildLayerStackStats:LayerStack layer_count:4,app_layer_count:3,gpu目标_index:3,显示类型:0logcat仅显示此项您只需在任务中复制粘贴我的全部代码即可