有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JSON解析在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);
    }
}

copyInputStreamToFile

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;
}

convertStringToJSON

private void convertStringToJSON(String json){
    try {
        jArr = new JSONArray(json);
    }
    catch (Exception e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

}

LogCat

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

有什么想法吗

谢谢


共 (0) 个答案