Android Restlet不返回JSON数据

时间:2018-05-31 13:15:52

标签: java restlet

我正在Android设备上创建一个网络服务器,除了杰克逊没有将我的对象转换为JSON之外,一切似乎都运行正常。当我在Android设备上从浏览器发出成功的(200)HTTP请求时,我在网络选项卡中没有获得JSON数据(查看Google Chrome中的响应)。我已经注册了Jackson Converter作为文档状态,以及之前的StackOverflow问题所涵盖的内容。这似乎不起作用,我没有收到任何错误。知道我做错了什么吗?我希望这里有足够的信息给你天才,否则让我知道你们还需要什么。

注意我也尝试过GSONConverter

编辑:我注意到响应中的实体始终为空

WebServer.java

package com.android.restlettest;

    import org.restlet.Component;
    import org.restlet.data.Protocol;

        public class WebServer {

            Component component;

            WebServer() {
                component = new Component();
                component.getServers().add(Protocol.HTTP, 9001);
                component.getDefaultHost().attach("", new MyApplication());
            }

            void start() {
                try {
                    System.out.println("Starting the WebServer");
                    component.start();
                } catch(Exception e) {
                    System.out.println("Exception caught: " + e);
                }
            }

            void stop() {

            }


        }

MyApplication.java

package com.android.restlettest;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
import org.restlet.engine.Engine;
import org.restlet.ext.gson.GsonConverter;
import org.restlet.ext.jackson.JacksonConverter;

public class MyApplication extends Application {

        MyApplication() {
            super();
            Engine.getInstance().getRegisteredConverters().add(new JacksonConverter());
            //Engine.getInstance().getRegisteredConverters().add(new GsonConverter());
        }

        @Override
        public synchronized Restlet createInboundRoot() {
            Router router = new Router(getContext());

            router.attach("/", IndexResource.class);
            router.attach("/report/log", LogServerResource.class);

            return router;
        }

    }

LogServerResource.java

 public class LogServerResource extends ServerResource implements ILogResource {

        @Get("json")
        public Log retrieve() {
            Log[] list = new Log[2];
            Log log1 = new Log("test.log", "alksdjf32984u23jfsdv", 0);
            list[0] = log1;
            return list[0];
            //return "{\"resource\": \"log\"}";
        }
    }

Log.java

package com.android.restlettest;

import java.io.Serializable;

    public class Log implements Serializable {
        private static final long serialVersionUID = 1L;

        public String data;
        public String name;
        public int id;

        public Log() {

        }

        public Log(final String name, final String data, final int id) {
            super();
            this.name = name;
            this.data = data;
            this.id = id;
        }
    }

1 个答案:

答案 0 :(得分:0)

我最终找到了一个与我有类似问题的人。我错过了一些需要添加的库。我为浪费你的时间而道歉。

Restlet server on Android: client is getting a null object