我实现了一个简单的Android Restlet服务器演示,可以将传感器值提供给Restlet客户端。但是,当restlet客户端接收到该对象时,该对象为null。我想这里并没有太多丢失,因为事务表明它已在服务器logcat中正确完成:

D/SensorTemperature: GET temperature: 20.0
W/System.err: 2015-10-23    20:28:37    192.168.2.129   -   -   8080    GET /sensors/temperature    -   200 -   0   198 http://192.168.2.94:8080    Restlet-Framework/2.3.5 -


在客户端,ClientResource打印(logcat):

ClientProxy for resource: GET http://192.168.2.94:8080/sensors/temperature HTTP/1.1 => HTTP/1.1 - OK (200) - The request has succeeded


此外,使用restlet请求/响应时,浏览器界面可以按预期工作(请参见下面的“ / test” :)。

我遵循了官方的Restlet教程:
http://restlet.com/technical-resources/restlet-framework/guide/2.2/introduction/first-steps/first-application

git:https://github.com/restlet/restlet-tutorial/tree/master/modules/org.restlet.tutorial.webapi/src/main/java/org/restlet/tutorial

连同此示例:
http://maxrohde.com/2011/09/02/restlet-quickstart/

代码:

这是restlet服务器:

public class ServerFactory {

    static {
        // Get NIO engines, instead of defaults
        Engine.getInstance().getRegisteredServers().add(new HttpsServerHelper(null));
        Engine.getInstance().getRegisteredServers().add(new HttpServerHelper(null));
        // Engine.setLogLevel(Level.FINEST);
        Engine.getInstance().getRegisteredConverters().add(new JacksonConverter());
    }

    private static Restlet restlet = new Restlet() {
        @Override
        public void handle(Request request, Response response) {
            Date date = new Date();
            float temp = WSDataProvider.getInstance().getTemperature();
            response.setEntity("Hello World!\nTime: " + date + "\nTemp: " + temp, MediaType.TEXT_PLAIN);
        }
    };

    public static Server createServer(int port, final String rootUri) {
        Component component = new Component();

        Server server = component.getServers().add(Protocol.HTTP, port);

        // Attach the sample application.
        component.getDefaultHost().attach("/test", restlet);

        SensorsApplication sensorsApp = new SensorsApplication();
        component.getDefaultHost().attach("/sensors", sensorsApp);

        return server;  // server.start() and server.stop() called via Android buttons
    }
}


和客户:

Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            Engine.getInstance().getRegisteredConverters().add(new JacksonConverter());

            // Initialize the resource proxy.
            final ClientResource cr = new ClientResource("http://192.168.2.94:8080/sensors/temperature");
            final SensorResource resource = cr.wrap(SensorResource.class);

            // Get the remote temperature sensor
            final SensorBase sensorBase = resource.retrieve();
            if (sensorBase != null)
                Log.e(TAG, "sensorBase.getData() = " + sensorBase.data);
            else
                Log.e(TAG, "sensorBase is null !!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start();


SensorBase类:(客户端+服务器)

public class SensorBase implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int TYPE_UNKNOWN = 0;
    public static final int TYPE_TEMPERATURE = 1;
    public static final int TYPE_HUMIDITY = 2;

    public int type;
    public float data;

    public SensorBase(final int type) {
        super();
        this.type = type;
    }
}


SensorResource类:(客户端+服务器)

public interface SensorResource {
    @Get
    public SensorBase retrieve();
}


SensorTemperature类:

public class SensorTemperature extends ServerResource implements SensorResource {

    private static final String TAG = "SensorTemperature";
    private static volatile SensorBase sensorBase = new SensorBase(SensorBase.TYPE_TEMPERATURE);

    public SensorBase retrieve() {
        float temp = WSDataProvider.getInstance().getTemperature();
        Log.d(TAG, "GET temperature: " + temp);
        sensorBase.data = temp;
        return sensorBase;
    }
}


传感器应用类:

public class SensorsApplication extends Application {
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/temperature", SensorTemperature.class);
        //TODO add more sensors
        return router;
    }
}


更新资料

我通过包含杰克逊jar文件com.fasterxml.jackson.core.jar修复了空指针异常。仅在Restlet客户端使用Engine.setLogLevel(Level.FINEST);时显示未找到类错误!

但是,我现在从客户端收到以下异常:

org.restlet.resource.ResourceException: Unprocessable Entity (422) - The server understands the content type of the request entity and the syntax of the request entity is correct but was unable to process the contained instructions


谢谢 !

最佳答案

实际上,这不是Restlet的问题,而是Jackson的问题。如果您查看422错误的根本原因,则会看到以下内容:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class SensorBase]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: sun.nio.ch.ChannelInputStream@1eb0d79; line: 1, column: 2]
 at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
 at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1071)
 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:264)
 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
 at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1269)
 at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:864)
 at org.restlet.ext.jackson.JacksonRepresentation.getObject(JacksonRepresentation.java:299)
 at org.restlet.ext.jackson.JacksonConverter.toObject(JacksonConverter.java:208)
 at org.restlet.service.ConverterService.toObject(ConverterService.java:229)
 at org.restlet.resource.Resource.toObject(Resource.java:889)
 ... 3 more


Jackson对对象SensorBase的反序列化在此类中需要一个空的构造函数:

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

    public static final int TYPE_UNKNOWN = 0;
    public static final int TYPE_TEMPERATURE = 1;
    public static final int TYPE_HUMIDITY = 2;

    public int type;
    public float data;

    public SensorBase() {

    }

    public SensorBase(final int type) {
        super();
        this.type = type;
    }

    (...)
}


希望对您有帮助,
蒂埃里

关于android - Android上的ReSTLet服务器:客户端获取空对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33311268/

10-13 03:00