我试图在我的Android应用程序中解析JSON,它在连接互联网时工作正常。但是,当我在没有互联网的情况下打开应用程序时,它会崩溃。
我正在调试代码,当它到达此部分jArr = new JSONArray(json);
时(在convertStringToJSON(json)
功能中),应用程序崩溃了。
if(MainActivity.isupdateAvailable) {
jsonparser.copyInputStreamToFile(songUrl, songFileName, mainActivity);
jArr = jsonparser.readFile(songFileName, mainActivity);
}
else{
jArr = jsonparser.readFile(songFileName, mainActivity);
}
}
public void copyInputStreamToFile( String url, String filename, Context context ) {
createConnection(url);
try {
FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
int line = 0;
int a = 0;
while ((line = reader.read()) != -1) {
fos.write(line);
a++;
}
fos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public JSONArray readFile(String filename, Context context){
try {
FileInputStream fis = context.openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
}
json = sb.toString();
convertStringToJSON(json);
fis.close();
is.close();
return jArr;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void convertStringToJSON(String json){
try {
jArr = new JSONArray(json);
}
catch (Exception e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
04-07 21:15:53.790 14868-14923/pk.org.ascii.ascii W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x415e1c80)
04-07 21:43:44.610 8094-8148/? E/OkHttpStack﹕ Invalid url / host name https://www.linkedin.com/voyager/api/lixTreatments?nc=1460047424620
04-07 21:43:44.620 8094-8145/? E/OkHttpStack﹕ Invalid url / host name https://www.linkedin.com/voyager/api/growth/pageContent/new-to-voyager?nc=1460047424621
04-07 21:43:44.630 8094-8150/? E/OkHttpStack﹕ Invalid url / host name https://www.linkedin.com/voyager/api/feed/updates?q=findRecentFeed&count=20&moduleKey=home-feed%3Aphone&queryAfterTime=1460044486149&tabType=feed&nc=1460047424631
04-07 21:43:44.650 8094-8149/? E/OkHttpStack﹕ Invalid url / host name https://www.linkedin.com/voyager/api/mux?nc=1460047424654
04-07 21:43:44.650 8094-8150/? E/OkHttpStack﹕ Invalid url / host name https://www.linkedin.com/voyager/api/growth/pageContent/voyager_abi_autosync_toast?nc=1460047424656
04-07 21:43:44.710 8094-8094/? E/LixManagerImpl﹕ Error in network response for lix type 1
这是堆栈跟踪,当我调试创建问题的代码时。
我不是因为它指的是LINKEDIN。
有什么想法吗?
由于