2015-08-29 137 views
0

我是新來的android,並試圖創建一個應用程序,使用後臺服務上運行的socket.io,但我得到一個錯誤,找不到在互聯網上的解決方案或我不明白這個問題,因爲我缺乏對Android(和對象)的經驗。java.lang.String不能轉換爲org.json.JSONObject

mSocket.on("start tracking", new Emitter.Listener() { 

      @Override 
      public void call(final Object... args) { 

       timer.scheduleAtFixedRate(new TimerTask() { 
       long t0 = System.currentTimeMillis(); 

        @Override 
        public void run() { 
         JSONObject data = (JSONObject) args[0]; 
         String id; 
         try { 
          id = data.getString("id"); 
         } catch (JSONException e) { 
          return; 
         } 
         //cancel(); 
         if (System.currentTimeMillis() - t0 > 60 * 1000) { 
          cancel(); 
         } else { 
          //do my stuff 
          String deviceId = Settings.System.getString(getContentResolver(), 
            Settings.System.ANDROID_ID); 
           if(id == deviceId){ 
           mSocket.emit("emmitting location", deviceId, latitude, longitude); 
           } 
          } 
         } 
        }, 0, 1000); 
      } 
     }); 

編輯: 我不知道我是不是過客JSON對象通過我的服務器。感謝@cybersam清除所有內容並耐心解釋整個情況。 :)

回答

1

String不是從JSONObject派生。因此,您需要將String實例轉換爲新的JSONObject實例。

試試這個:

mSocket.on("start tracking", new Emitter.Listener() { 

     @Override 
     public void call(final Object... args) { 

      timer.scheduleAtFixedRate(new TimerTask() { 
       long t0 = System.currentTimeMillis(); 

       @Override 
       public void run() { 
        JSONObject data; 
        String id; 
        try { 
         data = new JSONObject((String) args[0]); 
         id = data.getString("id"); 
        } catch (JSONException e) { 
         return; 
        } 
        //cancel(); 
        if (System.currentTimeMillis() - t0 > 60 * 1000) { 
         cancel(); 
        } else { 
         //do my stuff 
         String deviceId = Settings.System.getString(getContentResolver(), 
           Settings.System.ANDROID_ID); 
         mSocket.emit("emmitting location", deviceId, latitude, longitude); 
        } 
       } 
      }, 0, 1000); 
     } 
    }); 

[編輯]

在另一方面,如果輸入的數據只是一個String,而不是一個JSON字符串,這樣做:

mSocket.on("start tracking", new Emitter.Listener() { 

     @Override 
     public void call(final Object... args) { 

      timer.scheduleAtFixedRate(new TimerTask() { 
      long t0 = System.currentTimeMillis(); 

       @Override 
       public void run() { 
        String id = (String) args[0]; 
        //cancel(); 
        if (System.currentTimeMillis() - t0 > 60 * 1000) { 
         cancel(); 
        } else { 
         //do my stuff 
         String deviceId = Settings.System.getString(getContentResolver(), 
           Settings.System.ANDROID_ID); 
         mSocket.emit("emmitting location", deviceId, latitude, longitude); 
        } 
       } 
      }, 0, 1000); 
     } 
    }); 

注意:您從未實際使用過任何地方的id ...

+0

未處理的異常:org.json。 JSONException –

+0

它不會工作。該應用程序不會崩潰,但沒有發送位置,所以我把'mSocket.emit(「new message」,e);'在catch(JSONException e)中'並得到這個'org.json.JSONException:Value c923f2446ae9d398 java.lang.String類型不能轉換爲JSONObject' –

+0

好吧,我想你使用的是https://github.com/socketio/socket.io-client-java(你應該在你的問題中說明)。您發出的'e'值可能不是合法的JSON字符串,而是「c923f2446ae9d398」。你需要確保你發出一個合法的JSON字符串(並且它必須包含一個'id'屬性)。 – cybersam

相關問題