试图在其他答案中找到,但仍然无法做到:
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;
}
}
以下是JSONParser.java(JSON - single file to use with android app)获取的JSON:
{
"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"
}
}
任何人都可以发现问题吗?
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): }