2012-04-20 3 views
0

서버로부터 JSON 파일을 구문 분석하고 속성을 SQLite 데이터베이스에 저장하는 IntentService 클래스가 있습니다. 하지만 시작할 때마다 앱이 다운되고 stacktrace에 오류가 표시됩니다. 아무도 나를 해결하는 데 도움이 될 수 있습니까? 여기 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

답변

1

알 수없는 호스트 예외로 인한 것입니다. 두 가지 방법으로 문제를 해결할 수 있습니다

    UnknownHostException 위해 try/catch 블록에 또 다른 예외 포수 추가의 DNS 캐시
  1. 를 새로 에뮬레이터 또는 장치를 다시 시작
  2. . 보다 바람직하게는 당신이 예를 들어 일반 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는 거기에서 인터넷의 권한을 가지고 있습니다). 오류는 sqlite에 저장하지 않고 웹 서비스를 호출 할 때 문제가 있음을 보여줍니다. 여기에 문제

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

당신이 당신의 manifest.xml에 네트워크 권한을 선언해야합니다

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