2

I a trying to parse JSON in my android application, It works fine when internet is connected. But when I open the application without internet it crashes.

I am debugging the code and when it reaches to this part jArr = new JSONArray(json); (in convertStringToJSON(json) funtion) , the app crashes.

Code from where the function is called

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

readFile

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)

EDIT

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

Here is the stack trace,When I debugged the code that is creating issue.

I don't why its referring to LINKEDIN.

Any idea?

Thanks

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Faraz Javed
  • 21
  • 1
  • 3
  • 6
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this. If you do not understand what the stack trace is telling you, edit your question and paste in the stack trace, plus point out what code in your question corresponds to lines in that stack trace. – CommonsWare Apr 07 '16 at 16:09
  • Thanks for your reply, I have edited my question. it just says `04-07 21:15:53.790 14868-14923/pk.org.ascii.ascii W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x415e1c80)` I might be wrong, Kindly help me to figure the error out. Thanks – Faraz Javed Apr 07 '16 at 16:19
  • 2
    That is not a Java stack trace. If you have "show only this app" filtering turned on in Android Studio's Android Monitor view (where you see LogCat), turn it off, but set an "Error" filter, and find the Java stack trace. – CommonsWare Apr 07 '16 at 16:21
  • If your app isn't using LinkedIn, then I think you have copied the wrong stacktrace, but those errors do make sense though, seeing as your device as no access to a network to resolve a hostname. – OneCricketeer Apr 07 '16 at 17:03
  • Are you sure that `new JSONArray(json);`is the source of the error? I am wondering because the LogCat says that the thread exited because of an uncaught exception. You are actually trying to catch an Exception there. – puelo Apr 07 '16 at 17:07
  • @cricket_007 I filtered the stack trace with my package and error, And then I didn't find any result. Don't know whats the issue. – Faraz Javed Apr 07 '16 at 17:13
  • You won't see `e.printStackTrace();` if you filter by error because that is written as a warning. Just change it back to DEBUG – OneCricketeer Apr 07 '16 at 17:13
  • @puelo Yes, its not returning any JSONArray – Faraz Javed Apr 07 '16 at 17:16

0 Answers0