Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何制作RPC-JSON java服务器_Java_Json_Rpc_Json Rpc - Fatal编程技术网

如何制作RPC-JSON java服务器

如何制作RPC-JSON java服务器,java,json,rpc,json-rpc,Java,Json,Rpc,Json Rpc,我在Android中有一个客户机RPC-JSON,我正在尝试创建一个带有Java()库的RPC-JSON服务器。这是官方的例子: // The JSON-RPC 2.0 Base classes that define the // JSON-RPC 2.0 protocol messages import com.thetransactioncompany.jsonrpc2.*; // The JSON-RPC 2.0 server framework package import com

我在Android中有一个客户机RPC-JSON,我正在尝试创建一个带有Java()库的RPC-JSON服务器。这是官方的例子:

// The JSON-RPC 2.0 Base classes that define the 
// JSON-RPC 2.0 protocol messages
import com.thetransactioncompany.jsonrpc2.*;

// The JSON-RPC 2.0 server framework package
import com.thetransactioncompany.jsonrpc2.server.*;

import java.text.*;
import java.util.*;


/**
 * Demonstration of the JSON-RPC 2.0 Server framework usage. The request
 * handlers are implemented as static nested classes for convenience, but in 
 * real life applications may be defined as regular classes within their old 
 * source files.
 *
 * @author Vladimir Dzhuvinov
 * @version 2011-03-05
 */ 
public class Example {


    // Implements a handler for an "echo" JSON-RPC method
    public static class EchoHandler implements RequestHandler {


        // Reports the method names of the handled requests
        public String[] handledRequests() {

            return new String[]{"echo"};
        }


         // Processes the requests
         public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {

             if (req.getMethod().equals("echo")) {

                 // Echo first parameter

                 List params = (List)req.getParams();

             Object input = params.get(0);

             return new JSONRPC2Response(input, req.getID());
            }
            else {

                // Method name not supported

                return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
        }
        }
    }


    // Implements a handler for "getDate" and "getTime" JSON-RPC methods
    // that return the current date and time
    public static class DateTimeHandler implements RequestHandler {


        // Reports the method names of the handled requests
    public String[] handledRequests() {

        return new String[]{"getDate", "getTime"};
    }


    // Processes the requests
    public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {

        if (req.getMethod().equals("getDate")) {

            DateFormat df = DateFormat.getDateInstance();

        String date = df.format(new Date());

        return new JSONRPC2Response(date, req.getID());

            }
            else if (req.getMethod().equals("getTime")) {

            DateFormat df = DateFormat.getTimeInstance();

        String time = df.format(new Date());

        return new JSONRPC2Response(time, req.getID());
            }
        else {

            // Method name not supported

        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
            }
        }
    }


    public static void main(String[] args) {

        // Create a new JSON-RPC 2.0 request dispatcher
    Dispatcher dispatcher =  new Dispatcher();


    // Register the "echo", "getDate" and "getTime" handlers with it
    dispatcher.register(new EchoHandler());
    dispatcher.register(new DateTimeHandler());


    // Simulate an "echo" JSON-RPC 2.0 request
    List echoParam = new LinkedList();
    echoParam.add("Hello world!");

    JSONRPC2Request req = new JSONRPC2Request("echo", echoParam, "req-id-01");
    System.out.println("Request: \n" + req);

    JSONRPC2Response resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);

    // Simulate a "getDate" JSON-RPC 2.0 request
    req = new JSONRPC2Request("getDate", "req-id-02");
    System.out.println("Request: \n" + req);

    resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);

    // Simulate a "getTime" JSON-RPC 2.0 request
    req = new JSONRPC2Request("getTime", "req-id-03");
    System.out.println("Request: \n" + req);

    resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);
    }
}
我在手册和谷歌中搜索,但我不明白如何才能做到服务器正在等待请求并发送响应。我怎么能做到