2012-04-20 48 views
0

我有一個IntentService類從服務器解析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) 

堆棧跟蹤顯示,誤差在此以下行:爲(VideoLocation VL:dbLocations){

+0

http://stackoverflow.com/questions/4427487/java-unknown-host-exception – Selvin 2012-04-20 10:06:27

回答

1

它由於未知的主機異常。您可以通過兩種方式解決這個問題:

  1. 重新啓動模擬器或設備刷新其DNS緩存
  2. 添加另一個例外捕手你的try/catch塊UnknownHostException。更優選我建議你添加一個普通Exception

例如:

... 
... 
catch (UnknownHostExceptione) { 
     Log.e("test", "unknown host connection error"); 
} 
catch (Exception) { 
     Log.e("test", "an error occurred: " + e.toString()); 
} 
0

檢查您的網絡連接,URL,mainfest.xml(具有u採取互聯網的許可,從那裏)。錯誤顯示您在調用Web服務時遇到問題,而不是在sqlite中存儲。問題就在這裏,

http://historyvision.solutions.smfhq.com/api/locations.json 
0

確保你在你的manifest.xml聲明網絡權限

<uses-permission android:name="android.permission.INTERNET"/>