データベースからデータを取得し、次の JSON を出力する Web サービスを呼び出しています。
{"eventId":"1","eventTitle":"opening ceremony","eventCategory":"store","eventSubCategory":"clothing","eventDescription":"This is an event description of some kid for the first event","eventDate":"13/05/2012","eventTime":"14:52","eventAddress":"49 somerset road","eventCity":"southsea","eventCountry":"UK","eventWebsite":"www.nxtldn.com","eventEmail":"lg@nxtldn.com","eventPhone":"07757491567","eventKeywords":"clothes, street, wear, heart, love"},{"eventId":"2","eventTitle":"cupcakes","eventCategory":"Store","eventSubCategory":"food","eventDescription":"This is an event description of some kid for the second event","eventDate":"17/05/2012","eventTime":"11:22","eventAddress":"12 cleveleys road","eventCity":"london","eventCountry":"UK","eventWebsite":"www.ashshort.com","eventEmail":"ashshort@short.com","eventPhone":"0778514562","eventKeywords":"cupcakes, store, london, hipster"}]
これを追加します( http://jsonlint.com/を使用してJSONが有効かどうかをテストしました):
{"Events":[{"eventId":"1","eventTitle":"opening ceremony","eventCategory":"store","eventSubCategory":"clothing","eventDescription":"This is an event description of some kid for the first event","eventDate":"13/05/2012","eventTime":"14:52","eventAddress":"49 somerset road","eventCity":"southsea","eventCountry":"UK","eventWebsite":"www.nxtldn.com","eventEmail":"lg@nxtldn.com","eventPhone":"07757491567","eventKeywords":"clothes, street, wear, heart, love"},{"eventId":"2","eventTitle":"cupcakes","eventCategory":"Store","eventSubCategory":"food","eventDescription":"This is an event description of some kid for the second event","eventDate":"17/05/2012","eventTime":"11:22","eventAddress":"12 cleveleys road","eventCity":"london","eventCountry":"UK","eventWebsite":"www.ashshort.com","eventEmail":"ashshort@short.com","eventPhone":"0778514562","eventKeywords":"cupcakes, store, london, hipster"}]}
AsyncTask を使用して呼び出しています
public class EventSync extends AsyncTask<String, Integer, EventsList> {
EventsList elist1 = new EventsList();
@Override
protected EventsList doInBackground(String... urls) {
String tempurl = urls[0];
String output = "";
InputStream is = null;
StringBuilder sb = new StringBuilder();
EventsList list = new EventsList();
try
{
URL url = new URL(tempurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.connect();
is = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
output = sb.toString();
String json = "{\"Events\":" + output + "}";
System.out.println(json);
Gson gson = new Gson();
// When debugging this is the line I believe it fails on, It points to a bit of memory but the list is empty.
EventsList jsonlist = gson.fromJson(json, EventsList.class);
return jsonlist;
}
@Override
protected void onPostExecute(EventsList list)
{
System.out.println("in on post");
for (Event event : list.getEvents())
{
System.out.println(event.getEventId());
}
}}
public class EventsList {
public EventsList(){
}
private List<Event> events;
public List<Event> getEvents()
{
return events;
}
public void setEventsList(List<Event> events)
{
this.events = events;
}}
public class Event {
private String eventId;
private String eventTitle;
private String eventCategory;
private String eventSubCategory;
private String eventDescription;
private String eventDate;
private String eventTime;
private String eventAddress;
private String eventCity;
private String eventCountry;
private String eventWebsite;
private String eventEmail;
private String eventPhone;
private String eventKeywords;
public Event()
{
}
// ALL GETS AND SETS
現時点では、カスタム アダプターを使用してリストビューを作成する前に、それが返されるかどうかをテストしようとしています。しかし残念なことに、次のエラーが発生しています。
11-10 23:37:08.315: E/AndroidRuntime(5592): FATAL EXCEPTION: main
11-10 23:37:08.315: E/AndroidRuntime(5592): java.lang.NullPointerException
11-10 23:37:08.315: E/AndroidRuntime(5592): at com.nxtldn.trill.EventSync.onPostExecute(EventSync.java:71)
11-10 23:37:08.315: E/AndroidRuntime(5592): at com.nxtldn.trill.EventSync.onPostExecute(EventSync.java:1)
11-10 23:37:08.315: E/AndroidRuntime(5592): at android.os.AsyncTask.finish(AsyncTask.java:631)
11-10 23:37:08.315: E/AndroidRuntime(5592): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-10 23:37:08.315: E/AndroidRuntime(5592): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-10 23:37:08.315: E/AndroidRuntime(5592): at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 23:37:08.315: E/AndroidRuntime(5592): at android.os.Looper.loop(Looper.java:137)
11-10 23:37:08.315: E/AndroidRuntime(5592): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-10 23:37:08.315: E/AndroidRuntime(5592): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 23:37:08.315: E/AndroidRuntime(5592): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 23:37:08.315: E/AndroidRuntime(5592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-10 23:37:08.315: E/AndroidRuntime(5592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-10 23:37:08.315: E/AndroidRuntime(5592): at dalvik.system.NativeStart.main(Native Method)
私はアンドロイドが初めてなので、これを行うためのより良い方法があるかどうか、または正しい方法が何であるかはわかりません。
GSONがnullポインターを受け入れずに返すのは、私のJSONの構造にありますか?
助けてくれる人に前もって感謝します:)