1

Tried to find in other answers, but still can't make it:

class GetUpdate extends AsyncTask<Void, Void, Void>{

    JSONObject jobj = null;

    @Override
    protected Void doInBackground(Void... params) {             

        //JSON
        JSONParser jparser = new JSONParser();
        JSONObject data = jparser.getJSONFromUrl("http://myserver.com/file.txt");                       

        String title2 = "";

        try {
            jobj = data.getJSONObject("party1");                
            title2 = jobj.getString("title");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //title = new String[] { title2,title2,title2,title2};
        Log.i("ABCDE", "Got the address: " + title2);

        return null;
    }

}

and here is the JSON that it get from with JSONParser.java (JSON - single file to use with android app):

    {
   "party1": {
      "title": "Screen no. 1",
      "address": "Sesame Street",
      "date": "01-01-2014",
      "image": "http://myserver.com/image.jpg",
      "destination": "somewhere"
   },
   "party2": {
      "title": "Screen no. 2",
      "address": "Oak Street",
      "date": "01-01-2014",
      "image": "http://myserver.com/image.jpg",
      "destination": "somewhere"
   }
}

Anyone can spot the problem?

edit1: LogCat:

    12-14 19:13:24.341: E/AndroidRuntime(22725): FATAL EXCEPTION: AsyncTask #1
12-14 19:13:24.341: E/AndroidRuntime(22725): Process: com.example.app, PID: 22725
12-14 19:13:24.341: E/AndroidRuntime(22725): java.lang.RuntimeException: An error occured while executing doInBackground()
12-14 19:13:24.341: E/AndroidRuntime(22725):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at java.lang.Thread.run(Thread.java:841)
12-14 19:13:24.341: E/AndroidRuntime(22725): Caused by: java.lang.NullPointerException
12-14 19:13:24.341: E/AndroidRuntime(22725):    at com.example.app.MainActivity$GetUpdate.doInBackground(MainActivity.java:78)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at com.example.app.MainActivity$GetUpdate.doInBackground(MainActivity.java:1)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-14 19:13:24.341: E/AndroidRuntime(22725):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-14 19:13:24.341: E/AndroidRuntime(22725):    ... 4 more
12-14 19:13:56.910: E/JSON Parser(22725): Error parsing data org.json.JSONException: Unterminated object at character 192 of {
12-14 19:13:56.910: E/JSON Parser(22725):    "party1": {
12-14 19:13:56.910: E/JSON Parser(22725):       "title": "Screen no. 1",
12-14 19:13:56.910: E/JSON Parser(22725):       "address": "Sesame Street",
12-14 19:13:56.910: E/JSON Parser(22725):       "date": "01-01-2014",
12-14 19:13:56.910: E/JSON Parser(22725):       "image": "http://myserver.com/image.jpg",
12-14 19:13:56.910: E/JSON Parser(22725):       "destination": "somewhere"
12-14 19:13:56.910: E/JSON Parser(22725):    },
12-14 19:13:56.910: E/JSON Parser(22725):    "party2": {
12-14 19:13:56.910: E/JSON Parser(22725):       "title": "Screen no. 2",
12-14 19:13:56.910: E/JSON Parser(22725):       "address": "Oak Street",
12-14 19:13:56.910: E/JSON Parser(22725):       "date": "01-01-2014",
12-14 19:13:56.910: E/JSON Parser(22725):       "image": "http://myserver.com/image.jpg",
12-14 19:13:56.910: E/JSON Parser(22725):       "destination": "somewhere"
12-14 19:13:56.910: E/JSON Parser(22725):    },
12-14 19:13:56.910: E/JSON Parser(22725):    "party3": {
12-14 19:13:56.910: E/JSON Parser(22725):       "title": "Screen no. 1",
12-14 19:13:56.910: E/JSON Parser(22725):       "address": "Sesame Street",
12-14 19:13:56.910: E/JSON Parser(22725):       "date": "01-01-2014",
12-14 19:13:56.910: E/JSON Parser(22725):       "image": "http://myserver.com/image.jpg",
12-14 19:13:56.910: E/JSON Parser(22725):       "destination": "somewhere"
12-14 19:13:56.910: E/JSON Parser(22725):    },
12-14 19:13:56.910: E/JSON Parser(22725):    "party4": {
12-14 19:13:56.910: E/JSON Parser(22725):       "title": "Screen no. 1",
12-14 19:13:56.910: E/JSON Parser(22725):       "address": "Sesame Street",
12-14 19:13:56.910: E/JSON Parser(22725):       "date": "01-01-2014",
12-14 19:13:56.910: E/JSON Parser(22725):       "image": "http://myserver.com/image.jpg",
12-14 19:13:56.910: E/JSON Parser(22725):       "destination": "somewhere"
12-14 19:13:56.910: E/JSON Parser(22725):    }
12-14 19:13:56.910: E/JSON Parser(22725): }
Community
  • 1
  • 1
jean d'arme
  • 4,033
  • 6
  • 35
  • 70
  • how is jobj initialized? – Freestyle076 Dec 14 '14 at 18:37
  • added whole class for better context – jean d'arme Dec 14 '14 at 19:17
  • could you add any error messages and point out the line that is throwing the error? – Freestyle076 Dec 14 '14 at 19:22
  • Done. Now whole LogCat is here. – jean d'arme Dec 14 '14 at 19:27
  • The error message says the json object is badly formatted by a missing terminating character, but I can't find a problem in the json object being printed. Take a look at character 192, see if the formatting has any problems – Freestyle076 Dec 14 '14 at 20:08
  • Well, I tried shorter version: {    "party1": {       "title": "Screen no. 1" } and it says that the 49th character is bad, but this example has only 48 characters so WTF??? – jean d'arme Dec 14 '14 at 23:17
  • that example is missing the final terminating '}', but the object in the OP doesn't seem to have a missing terminating '}' – Freestyle076 Dec 14 '14 at 23:37
  • Okay, that was right. In that particular example I missed temrinating '}', but know I got 'JSONException: No value for party1' - should 'party1' have value if I want it just to identify object? – jean d'arme Dec 14 '14 at 23:43
  • I got this working deleting all the whitespaces. So it is not good to keep JSON file formatted for human eye? – jean d'arme Dec 14 '14 at 23:52
  • 1
    I can't believe I didn't notice the .txt file suffix... No, plain text can't be used to store json objects. If the file were in JSON format (.json) this should work, the nicely formatted output that you tend to see json in is a product of printing functions – Freestyle076 Dec 14 '14 at 23:55
  • Wow, it works now when I changed from .txt to .json. I now got "12-15 00:57:34.270: E/JSON Parser(30530): Error parsing data org.json.JSONException: Value – jean d'arme Dec 14 '14 at 23:58
  • Okay, I got it. It only reads it properly when it's single line. Human-friendly version of JSON doesn't work for me - don't know why. – jean d'arme Dec 15 '14 at 00:13
  • The implicit space and newline characters to the human friendly form. They're native to text but not JSON – Freestyle076 Dec 15 '14 at 01:34

0 Answers0