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"] ["レギュラーチケット"]
それは何かを返すことを意味します。