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): }
13
  • how is jobj initialized? Commented Dec 14, 2014 at 18:37
  • added whole class for better context Commented Dec 14, 2014 at 19:17
  • could you add any error messages and point out the line that is throwing the error? Commented Dec 14, 2014 at 19:22
  • Done. Now whole LogCat is here. Commented Dec 14, 2014 at 19:27
  • 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 Commented Dec 14, 2014 at 23:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.