サーバーから JSON ファイルを解析し、属性を SQLite データベースに保存する IntentService クラスがあります。しかし、起動するたびにアプリがクラッシュし、スタックトレースにエラーが表示されます。誰でもエラーを解決するのを手伝ってもらえますか? ありがとう
これが私のクラスです:
public class DBSync extends IntentService {
//holds new Location[] temporarily, well be released after storing data to DB
public static VideoLocation[] videoLocations = null;
private static VideoLocationReceiver receiver = null;
public DBSync() {
super("DBSync");
}
@Override
protected void onHandleIntent(Intent intent) {
VideoLocationList locations = null;
videoLocations = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(
"http://historyvision.solutions.smfhq.com/api/locations.json");
try {
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream instream = response.getEntity().getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(
instream, "UTF-8"), 8000);
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
instream.close();
//parse to gson here
String JSON_Result = total.toString();
JSON_Result = "{ locations:"+JSON_Result+"}";
Log.d("DBSync", "JSON_Result:"+JSON_Result);
Gson gson = new Gson();
locations = (VideoLocationList)gson.fromJson(JSON_Result, VideoLocationList.class);
VideoLocationItem[] vidLocItems = locations.getVideoLocations();
int numLocations = vidLocItems.length;
videoLocations = new VideoLocation[numLocations];
for(int i=0; i<vidLocItems.length; i++ ){
videoLocations[i] = vidLocItems[i].location;
}
if (receiver!=null) {
receiver.receivedVideoLocationData(videoLocations);
}
// Log.d("DBSync", "resulting json: "+gson.toJson(locations, VideoLocationList.class));
}
} catch (ClientProtocolException e) {
// TODO HANDLE THIS EXCEPTION, E.G. CHECK NETWORK/INFORM USER ETC.
receiver.receivedVideoLocationData(null);
e.printStackTrace();
} catch (IOException e) {
// TODO HANDLE THIS EXCEPTION, E.G. CHECK NETWORK/INFORM USER ETC.
e.printStackTrace();
}
//only after sucessfully retrieving the remote json we open and update the db
if (videoLocations != null) {
JsonDB dbhelper = WWHApplication.getInstance().getJsonDBInstance();
SQLiteDatabase db = dbhelper.getWritableDatabase();
//begin transaction for insert
db.beginTransaction();
dbhelper.InsertLocationArray(db,videoLocations);
db.setTransactionSuccessful();//end transaction
db.endTransaction();
}
//fetch db content just for debugging
JsonDB dbhelper = WWHApplication.getInstance().getJsonDBInstance();
SQLiteDatabase db = dbhelper.getWritableDatabase();
VideoLocation[] dbLocations = dbhelper.getVideoLocations(db);
Gson gs = new Gson();
for(VideoLocation vl : dbLocations){
Log.d("DBSync", gs.toJson(vl));
}
}
public static VideoLocation[] getVideoLocations(){
return videoLocations;
}
public static void setVideoLocationReceiver(VideoLocationReceiver vr){
receiver = vr;
}
public interface VideoLocationReceiver {
public void receivedVideoLocationData(final VideoLocation[] vidLocs);
}
}
スタックトレースは次のとおりです。
04-20 11:20:32.960: W/System.err(2732): java.net.UnknownHostException: historyvision.solutions.smfhq.com
04-20 11:20:32.960: W/System.err(2732): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
04-20 11:20:32.960: W/System.err(2732): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
04-20 11:20:32.960: W/System.err(2732): at java.net.InetAddress.getAllByName(InetAddress.java:256)
04-20 11:20:32.960: W/System.err(2732): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
04-20 11:20:32.960: W/System.err(2732): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-20 11:20:32.960: W/System.err(2732): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-20 11:20:32.960: W/System.err(2732): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
04-20 11:20:32.960: W/System.err(2732): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-20 11:20:32.960: W/System.err(2732): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-20 11:20:32.960: W/System.err(2732): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-20 11:20:32.960: W/System.err(2732): at com.wwh.Sync.DBSync.onHandleIntent(DBSync.java:49)
04-20 11:20:32.960: W/System.err(2732): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
04-20 11:20:32.960: W/System.err(2732): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 11:20:32.960: W/System.err(2732): at android.os.Looper.loop(Looper.java:130)
04-20 11:20:32.960: W/System.err(2732): at android.os.HandlerThread.run(HandlerThread.java:60)
04-20 11:20:33.040: W/dalvikvm(2732): threadid=9: thread exiting with uncaught exception (group=0x40015560)
04-20 11:20:33.040: E/AndroidRuntime(2732): FATAL EXCEPTION: IntentService[DBSync]
04-20 11:20:33.040: E/AndroidRuntime(2732): java.lang.NullPointerException
04-20 11:20:33.040: E/AndroidRuntime(2732): at com.wwh.Sync.DBSync.onHandleIntent(DBSync.java:110)
04-20 11:20:33.040: E/AndroidRuntime(2732): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
04-20 11:20:33.040: E/AndroidRuntime(2732): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 11:20:33.040: E/AndroidRuntime(2732): at android.os.Looper.loop(Looper.java:130)
04-20 11:20:33.040: E/AndroidRuntime(2732): at android.os.HandlerThread.run(HandlerThread.java:60)
スタック トレースは、次の行でエラーを示しています: for(VideoLocation vl : dbLocations){