【问题标题】:java.lang.String cannot be cast to org.json.JSONObjectjava.lang.String 不能转换为 org.json.JSONObject
【发布时间】:2015-08-29 01:20:10
【问题描述】:

我是 android 新手,正在尝试创建一个应用程序,使用在后台服务上运行的 socket.io,但我收到错误并且无法在 Internet 上找到解决方案,或者我对问题的理解不够因为我缺乏使用 android(和对象)的经验。

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 清理一切并耐心解释整个情况。 :)

【问题讨论】:

    标签: android json object socket.io


    【解决方案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...

    【讨论】:

    • 未处理的异常:org.json.JSONException
    • 这行不通。该应用程序不会崩溃但没有发送位置,所以我将mSocket.emit("new message", e); 放入catch(JSONException e) 并得到了这个org.json.JSONException: Value c923f2446ae9d398 of type java.lang.String cannot be converted to JSONObject
    • 好的,我猜你正在使用github.com/socketio/socket.io-client-java(你应该在你的问题中说明)。您发出的 e 值可能不是合法的 JSON 字符串,而是“c923f2446ae9d398”。您需要确保发出合法的 JSON 字符串(并且它必须包含 id 属性)。
    • 是的,我想我应该提到这一点,但我认为 .emit 的东西很明显。是的,它不是一个 json 字符串,只是一个值。这是我在服务器上的 js 代码socket.on('starting tracking', function(id){io.emit('start tracking', id);});
    • 那么,您为什么要将非 JSON 字符串视为 JSON 字符串?
    【解决方案2】:

    Java 似乎在处理 JSONObject 实例化中的字符串转换方面存在限制。

    所以你最好使用 java String.valueOf() 方法为你转换做这样的事情:

    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 {
                        // Update conversion like this below
                        data = new JSONObject(String.valueOf(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); 
        } 
    });
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2018-02-10
      • 1970-01-01
      • 2015-05-18
      • 2011-10-27
      • 2019-10-19
      • 2018-01-23
      • 2011-05-09
      • 2014-06-24
      相关资源
      最近更新 更多