Я хочу распарсить вот это https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json.
Столкнулся с проблемой, что моя программа не заходит в цикл для создания списка стран. После попадания в блок onPostExecute
, программа попадает в Exception
. Подскажите что не так?
public class MainActivity extends AppCompatActivity {
ArrayList dataEntries;
ListView list;
public static String LOG_TAG = "my_log";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
new ParseAsyncTask().execute();
}
private class ParseAsyncTask extends AsyncTask<Void, Void, String> {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String resultJson = "";
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL("https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resultJson = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return resultJson;
}
@Override
protected void onPostExecute(String strJson) {
super.onPostExecute(strJson);
Log.d(LOG_TAG, strJson);
JSONObject dataJsonObj = null;
try {
JSONArray data = new JSONArray(strJson.toString());
// dataJsonObj = new JSONObject(strJson);
// JSONArray object = dataJsonObj.getJSONArray(resultJson);
// Log.d(LOG_TAG, "222222222222" + object);
for (int i = 0; i < data.length(); i++) {
JSONObject dataEntry = data.getJSONObject(i);
Iterator<String> key = dataEntry.keys();
Map<String, String> entry = new HashMap<>();
while (key.hasNext()){
String nextKey = key.next();
entry.put(nextKey, dataEntry.getString(nextKey));
}
dataEntries.add(entry);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Логи:
07-05 22:58:19.011 19319-19319/com.example.loc_by.newjson D/my_log: {"China":["Guangzhou","Fuzhou","Beijing","Baotou","Hohhot","Guiyang","Yinchuan","Nanjing","Changzhou","Chuzhou","Hefei","Jinan","Qingdao","Harbin","Zhaodong","Taiyuan","Xi'an","Xianyang","Shenzhen","Nanning","Zhengzhou","Xinxiang","Luohe","Luoyang","Chaoyang","Xingyi","Foshan","Haikou","Chengdu","Dongguan","Mingzhou","Chongqing","Zhuhai","Kunming","Wuhan","Xiling","Huizhou","Jiangmen","Shantou","Changxiacun","Zhongshan","Lhasa","Nanchang","Tianjin","Shanghai","Hebei","Shijiazhuang","Quanzhou","Putian","Xiamen","Chengyang","Zhangzhou","Sanming","Nanping","Baoding","Langfang","Yantai","Binzhou","Lanzhou","Yueqing","Zhongxin","Zhoushan","Hangzhou","Ningbo","Wenzhou","Changchun","Fuyang","Jieshou","Anqing","Wuhu","Shishi","Shishi","Weitang","Shenyang","Changsha","Yongjiawan","Lengshuijiang","Shijiazhuang","Xuchang","Suzhou","Xuzhou","Taizhou","Nanyang","Xinhua","Ürümqi","Yan'an Beilu","Baotao","Macao","Wuxi","Yangzhou","Baiyin","Tongren","Kunshan","Zhangjiagang","Jiangyin","Zhenjiang","Zhoukou","Anyang","Dalian","Tongcun","Shihezi","Xining","Zhangye","Qingyang","Wangsu","Anshun","Kaili","Bijie","Shuigang","Jianyuan","Jinlong","Jingdezhen","Yichang","Xiangfan","Jiayuan","Shashi","Yingjie","Huangshi","Jinhua","Zhengyuan","Langli","Hengyang","Jianning","Xiangtan","Rongxing","Xincao","Jinda","Nanlong","Xiangcheng","Nanma","Zhongxiang","Pudong","Shuitou","Zhenxing","Jinjiang","Longxun","Guanqiao","Jingfeng","Shijing","Tangbian","Jiangchuanlu","Jiaoyun","Guizhoumanzuxiang","Qingzhen","Changde","Xianning","Jiaozhou","Weifang","Tai'an","Luoxi","Guoji","Guangdong","Sijiqing","Huzhou","Panjin Shi","Daqing","Jilin City","Lianyungang","Yancheng","Yuehu","Kunshan","Taicang","Lianshui","Nantong","Jiaotong","Changshu City","Xingxiangcun","Jinsha","Jiangyan","Chaigoubu","Ma'anshan","Huainan","Haibei","Shenlong","Nangxian","Rongsheng","Changfeng","Chengqiao","Jiafu","Shenzhou","Shantou","Qingyuan","Gyari","Xinshijie","Zhaoqing","Zhanjiang","Kuicheng","Taoyuan","Jincheng","Caishen","Shiyan","Liaoyang","Xingtai","Wenchang","Wanning","Qionghai","Huilongba","Dingcheng","Baodian","Wuzhishan","Chengmai","Yinggen","Ledong","Lincheng","Baisha","Changjiang","Dongfang","Changjian","Jinmao","Yangpu","Baipo","Jiefang","Danzhou","Lingshui","Haidian","Sanya","Rongjiang","Longyan","Jinghe","Zhangjiakou","Renqiu","Yaocheng","Kaifeng","Hebi","Jiaozuo","Pingdingshan","Anshan","Dandong","Haitang","Tongchuan","Ankang","Guozhen","Shangluo","Xijing","Weinan","Yulin","Yining","Dingxi","Wuwei","Dawukou","Lishui","Quzhou","Hejiang","Handan","Qinhuangdao","Hengshui","Longxin","Wen'an","Yichun","Heihe","Jiaxing","Korla","Kuytun","Di'an","Yu'an","Mengzhou","Hulu","Yizhou","Shuliang","Shancheng","Fushun","Dashiqiao","Laonian","Shengli","Wenquan","Zhiye","Lingzhi","Zhongtang","Gucheng","Xinhua","Ninghe","Dangyang","Yizhong","Jizhou","Tianbao","Jinghai","Julong","Jiaqin","Jiayue","Dabaizhuang","Juchuan","Hexi","Jinnan","Hangu","Nankai","Hedong","Yanglou","Huawei","Hanting","Tianshi","Baiyu","Bohai","Rujia","Tongwang","Meijiang","Dagang","Baodi","Daqiuzhuang","Yuxi","Zicheng","Shaoxing","Zhoushan","Xiaoshan","Linhai","Cixi","Jinchuan","Zhaobaoshan","Tiangang","Beilun","Zhangqi","Zhenghai","Cicheng","Lishu","Chengbei","Heyi","Xikou","Jiangkou","Shunshui","Simen","Yuyao","Lanjiang","Jiangdong","Gaotang","Xiangshan","Shipu","Jinyi","Chengzhong","Yinzhou","Luoyang","Mapai","Cangnan","Jinxiangzhen","Yingjia","Pingyang","Rui'an","Tianfu","Shangtang","Yongjia","Tiancheng","Hongqiao","Furong","Wenxing","Mingxi","Jinshan","Changtu","Anzi","Xianren","Zhongxing","Guanli","Yucai","Xianjiang","Aojiang","Dongtou","Rongjiang","Jinmen","Qiantang","Baojiang","Huling","Liushi","Yuecheng","Hongyun","Longhua","Yajin","Simcun","Longgang","Yingdu","Wansong","Yuele","Nanjiang","Longhu","Ningyi","Fengling","Wuzhou","Xinchen","Jinghu","Fangzhuang","Yinfang","Cili","Angu","Feiyun","Wanquan","Kunyang","Shibei","Jiangnan","Yujing","Yishan","Xuefeng","Feilong","Shangrao","Xuexiao","Yuzhen","Huangbao","Longquan","Pizhou","Songyang","Qingtian","Chenguang","Kaiy
07-05 22:58:19.246 19319-19319/com.example.loc_by.newjson W/System.err: org.json.JSONException: Value {"China":["Guangzhou","Fuzhou","Beijing","Baotou","Hohhot","Guiyang","Yinchuan","Nanjing","Changzhou","Chuzhou","Hefei","Jinan","Qingdao","Harbin","Zhaodong","Taiyuan","Xi'an","Xianyang","Shenzhen","Nanning","Zhengzhou","Xinxiang","Luohe","Luoyang","Chaoyang","Xingyi","Foshan","Haikou","Chengdu","Dongguan","Mingzhou","Chongqing","Zhuhai","Kunming","Wuhan","Xiling","Huizhou","Jiangmen","Shantou","Changxiacun","Zhongshan","Lhasa","Nanchang","Tianjin","Shanghai","Hebei","Shijiazhuang","Quanzhou","Putian","Xiamen","Chengyang","Zhangzhou","Sanming","Nanping","Baoding","Langfang","Yantai","Binzhou","Lanzhou","Yueqing","Zhongxin","Zhoushan","Hangzhou","Ningbo","Wenzhou","Changchun","Fuyang","Jieshou","Anqing","Wuhu","Shishi","Shishi","Weitang","Shenyang","Changsha","Yongjiawan","Lengshuijiang","Shijiazhuang","Xuchang","Suzhou","Xuzhou","Taizhou","Nanyang","Xinhua","Ürümqi","Yan'an Beilu","Baotao","Macao","Wuxi","Yangzhou","Baiyin","Tongren","Kunshan","Zhangjiagang","Jiangyin","Zhenjiang","Zhoukou","Anyang","Dalian","Tongcun","Shihezi","Xining","Zhangye","Qingyang","Wangsu","Anshun","Kaili","Bijie","Shuigang","Jianyuan","Jinlong","Jingdezhen","Yichang","Xiangfan","Jiayuan","Shashi","Yingjie","Huangshi","Jinhua","Zhengyuan","Langli","Hengyang","Jianning","Xiangtan","Rongxing","Xincao","Jinda","Nanlong","Xiangcheng","Nanma","Zhongxiang","Pudong","Shuitou","Zhenxing","Jinjiang","Longxun","Guanqiao","Jingfeng","Shijing","Tangbian","Jiangchuanlu","Jiaoyun","Guizhoumanzuxiang","Qingzhen","Changde","Xianning","Jiaozhou","Weifang","Tai'an","Luoxi","Guoji","Guangdong","Sijiqing","Huzhou","Panjin Shi","Daqing","Jilin City","Lianyungang","Yancheng","Yuehu","Kunshan","Taicang","Lianshui","Nantong","Jiaotong","Changshu City","Xingxiangcun","Jinsha","Jiangyan","Chaigoubu","Ma'anshan","Huainan","Haibei","Shenlong","Nangxian","Rongsheng","Changfeng","Chengqiao","Jiafu","Shenzhou","Shantou","Qingyuan","Gyari","Xinshijie","Zhaoqing","Zhanjiang","Kuicheng","Taoyuan","Jincheng","Caishen","Shiyan","Liaoyang","Xingtai","Wenchang","Wanning","Qionghai","Huilongba","Dingcheng","Baodian","Wuzhishan","Chengmai","Yinggen","Ledong","Lincheng","Baisha","Changjiang","Dongfang","Changjian","Jinmao","Yangpu","Baipo","Jiefang","Danzhou","Lingshui","Haidian","Sanya","Rongjiang","Longyan","Jinghe","Zhangjiakou","Renqiu","Yaocheng","Kaifeng","Hebi","Jiaozuo","Pingdingshan","Anshan","Dandong","Haitang","Tongchuan","Ankang","Guozhen","Shangluo","Xijing","Weinan","Yulin","Yining","Dingxi","Wuwei","Dawukou","Lishui","Quzhou","Hejiang","Handan","Qinhuangdao","Hengshui","Longxin","Wen'an","Yichun","Heihe","Jiaxing","Korla","Kuytun","Di'an","Yu'an","Mengzhou","Hulu","Yizhou","Shuliang","Shancheng","Fushun","Dashiqiao","Laonian","Shengli","Wenquan","Zhiye","Lingzhi","Zhongtang","Gucheng","Xinhua","Ninghe","Dangyang","Yizhong","Jizhou","Tianbao","Jinghai","Julong","Jiaqin","Jiayue","Dabaizhuang","Juchuan","Hexi","Jinnan","Hangu","Nankai","Hedong","Yanglou","Huawei","Hanting","Tianshi","Baiyu","Bohai","Rujia","Tongwang","Meijiang","Dagang","Baodi","Daqiuzhuang","Yuxi","Zicheng","Shaoxing","Zhoushan","Xiaoshan","Linhai","Cixi","Jinchuan","Zhaobaoshan","Tiangang","Beilun","Zhangqi","Zhenghai","Cicheng","Lishu","Chengbei","Heyi","Xikou","Jiangkou","Shunshui","Simen","Yuyao","Lanjiang","Jiangdong","Gaotang","Xiangshan","Shipu","Jinyi","Chengzhong","Yinzhou","Luoyang","Mapai","Cangnan","Jinxiangzhen","Yingjia","Pingyang","Rui'an","Tianfu","Shangtang","Yongjia","Tiancheng","Hongqiao","Furong","Wenxing","Mingxi","Jinshan","Changtu","Anzi","Xianren","Zhongxing","Guanli","Yucai","Xianjiang","Aojiang","Dongtou","Rongjiang","Jinmen","Qiantang","Baojiang","Huling","Liushi","Yuecheng","Hongyun","Longhua","Yajin","Simcun","Longgang","Yingdu","Wansong","Yuele","Nanjiang","Longhu","Ningyi","Fengling","Wuzhou","Xinchen","Jinghu","Fangzhuang","Yinfang","Cili","Angu","Feiyun","Wanquan","Kunyang","Shibei","Jiangnan","Yujing","Yishan","Xuefeng","Feilong","Shangrao","Xuexiao","Yuzhen","Huangbao","Longquan","Pizhou","Song
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at com.example.loc_by.newjson.MainActivity$ParseAsyncTask.onPostExecute(MainActivity.java:80)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at com.example.loc_by.newjson.MainActivity$ParseAsyncTask.onPostExecute(MainActivity.java:40)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:660)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at android.os.Looper.loop(Looper.java:154)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-05 22:58:19.248 19319-19319/com.example.loc_by.newjson W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
07-05 22:58:19.249 19319-19319/com.example.loc_by.newjson W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Вам нужно пройти по всем ключам корневого JsonObject
и достать с каждого JsonArray
. Пример:
try {
JSONObject data = new JSONObject(jsonStr);
for (Iterator<String> countries = data.keys(); countries.hasNext();) { // Цикл по каждой стране
String country = countries.next(); // Страна
JSONArray cities = data.getJSONArray(country); // Массив с городами для этой страны
}
} catch (JSONException e) {
e.printStackTrace();
}
проблема в том что strJson - ето JSONObject, а не JSONArray.
JSONArray data = new JSONArray(strJson.toString());
тебе нужно что-то примерно такое:
JSONObject response = new JSONObject(strJson);
JSONArray chinaArray= response.getJSONArray("China");
// далее можно будет работать как с масивой
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Есть файл из которого я считаю пути к файлам, заношу в список и дальше считаю сколько в этих путях файлов
Начал изучать android Такая проблема, не вызывается метод onCreate()Создал тестовую активность TestActivity
ЗдравствуйтеУ меня есть черно-белое изображение в BufferedImage