我想从 web 服务中获取 json 对象并将它们显示在列表视图中,
这是我的代码:
public class MainActivity extends Activity {
private JsonObject data = null;
private JsonObject response = null;
private JsonArray records = null;
private JsonObject record = null;
private ArrayList<SearchResults> results = null;
SearchResults sr1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private ArrayList<SearchResults> GetSearchResults() {
results = new ArrayList<SearchResults>();
new GetResults().execute("");
return results;
}
private class GetResults extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Map<String, String> callArgs = new HashMap<String, String>(1);
callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
try {
response = EventPulseCloud.call("ListEvents", callArgs);
} catch (HttpClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JsonException e) {
e.printStackTrace();
}
return response.get("Type").toString();
}
protected void onPostExecute(String result) {
if(result.equals("success")) {
data = (JsonObject) response.get("Data");
records = (JsonArray) data.get("Records");
try {
records = response.getObject ("Data").getArray ("Records");
} catch (JsonException e) {
e.printStackTrace();
}
for(int i = 0; i < records.count(); i ++) {
record = (JsonObject) records.get(i);
sr1 = new SearchResults();
sr1.setAddress(record.get("address").toString());
sr1.setStartingDate(record.get("StartingDate").toString());
sr1.setTicketCategories(record.get("ticketCategories").toString());
results.add(sr1);
}
}
}
}
}
我在 logcat 中得到了这个:
06-05 15:53:39.765: E/AndroidRuntime(10012): FATAL EXCEPTION: AsyncTask #1
06-05 15:53:39.765: E/AndroidRuntime(10012): java.lang.RuntimeException: An error occured while executing doInBackground()
06-05 15:53:39.765: E/AndroidRuntime(10012): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-05 15:53:39.765: E/AndroidRuntime(10012): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.lang.Thread.run(Thread.java:856)
06-05 15:53:39.765: E/AndroidRuntime(10012): Caused by: java.lang.NullPointerException
06-05 15:53:39.765: E/AndroidRuntime(10012): at com.example.listview.MainActivity$GetResults.doInBackground(MainActivity.java:73)
06-05 15:53:39.765: E/AndroidRuntime(10012): at com.example.listview.MainActivity$GetResults.doInBackground(MainActivity.java:1)
06-05 15:53:39.765: E/AndroidRuntime(10012): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-05 15:53:39.765: E/AndroidRuntime(10012): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-05 15:53:39.765: E/AndroidRuntime(10012): ... 5 more
06-05 15:53:40.050: I/Process(10012): Sending signal. PID: 10012 SIG: 9
在 logcat 中,MainActivity.java:73行是:
return response.get("Type").toString();
一切对我来说似乎都是正确的......拜托,你对此有什么想法吗?谢谢 :)
编辑: 当我在 java 中尝试这段代码时:
Map<String, String> callArgs = new HashMap<String, String>(1);
callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
response = EventPulseCloud.call("ListEvents", callArgs);
if(response.get("Type").equals("success")) {
JsonArray records = response.getObject ("Data").getArray ("Records");
for (int i = 0; i < records.count (); i++) {
JsonObject record = (JsonObject)records.get (i);
System.out.print(record.get("ticketCategories").toString());
}
}
else
System.out.println("No");
它显示在屏幕上:
["SingleDay"] ["SingleDay"] ["普通票"]
它确实意味着它返回了一些东西。