Calling mining with jsonrpc
Publish: 2021-05-27 01:56:53
1. Send according to the standard JSON string format
JSON RPC is a standard
wiki.geekdream.com/specification/json RPC_ 2.0. HTML
here is the official document of 2.0. In addition, the package for parsing needs to be introced
JSON RPC is a standard
wiki.geekdream.com/specification/json RPC_ 2.0. HTML
here is the official document of 2.0. In addition, the package for parsing needs to be introced
2. jsonrpc = new JSONRpcClient("/ json-rpc"); // Instantiate JSON remote call
3. jQuery.post(url, [data], [callback], [type]
$.post(" test.php" name: " John" time: " 2pm"});
$.post(" test.php" name: " John" time: " 2pm"});
4. JSONObject jsonObject = new JSONObject();
Map map = request.getParameterMap();
Iterator<?> it = map.keySet().iterator();
while(it.hasNext()){
String key = (String)it.next();
String[] values = (String[])map.get(key);
jsonObject.accumulate(key, values[0]);
}
String name = jsonObject.getString("userName"); //返回从前台接受的用户名
System.out.println(name); //输出用户名
jsonObject.clear(); // 清空jsonObjec中的数据
jsonObject.put("love" , "足球"); //将足球赋给love这个变量名
out.print(jsonObject); //返回json格式的数据
Map map = request.getParameterMap();
Iterator<?> it = map.keySet().iterator();
while(it.hasNext()){
String key = (String)it.next();
String[] values = (String[])map.get(key);
jsonObject.accumulate(key, values[0]);
}
String name = jsonObject.getString("userName"); //返回从前台接受的用户名
System.out.println(name); //输出用户名
jsonObject.clear(); // 清空jsonObjec中的数据
jsonObject.put("love" , "足球"); //将足球赋给love这个变量名
out.print(jsonObject); //返回json格式的数据
5. Json-rpc-java is composed of two components for user visualization. They are jsonrpcbridge and jsonrpcservlet. The coordination of the two components realizes the processing of request objects by JSON server and responds to users
jsonrpcbridge is a session object that has the reference of the object exported from the server to a specific client. It receives the json-rpc request from the server (that is, jsonrpcservelet), and then decodes the JSON object into Java object, and then plays the role of method call, In addition, the Java object result group code (Marshall) returned by the method call is passed to the client for processing as a JSON object. The specific type conversion from Java object to JavaScript object is handled by serializer, which is responsible for serialization
jsonrpcbridge must be placed in the httpsession object, and the registered property name is "jsonrpcbridge", so that jsonrpcservlet can locate the bridge responsible for calling Java objects exported to the client. Therefore, in order to export all instances and static methods of an object to the client, you should have the following code: jsonrpcbridge. Registerobject (& quot; myObject", myObject)
in order to export all static methods of a class, you should: jsonrpcbridge. Registerclass (& quot; myObject", myObject)
if registerobject and registerclass are called multiple times to be used by objects with the same key value, they will be updated by the latest assigned object
in JSON, you can also use global bridge in singleton mode to export all instance methods for all HTTP clients. It can be used to register factory class, but we should pay attention to authentication and security when using it. When used, the identifier is as follows: jsonrpcbridge. Getglobalbridge(). Registerobject (& quot; myObject", myObject); As above, it can also export all static methods
the most important part of this protocol is servlet. Now let's briefly introce it:
in this protocol, jsonrpcservelet acts as a transmitter to process the JSON transmitted over HTTP
jsonrpcbridge is a session object that has the reference of the object exported from the server to a specific client. It receives the json-rpc request from the server (that is, jsonrpcservelet), and then decodes the JSON object into Java object, and then plays the role of method call, In addition, the Java object result group code (Marshall) returned by the method call is passed to the client for processing as a JSON object. The specific type conversion from Java object to JavaScript object is handled by serializer, which is responsible for serialization
jsonrpcbridge must be placed in the httpsession object, and the registered property name is "jsonrpcbridge", so that jsonrpcservlet can locate the bridge responsible for calling Java objects exported to the client. Therefore, in order to export all instances and static methods of an object to the client, you should have the following code: jsonrpcbridge. Registerobject (& quot; myObject", myObject)
in order to export all static methods of a class, you should: jsonrpcbridge. Registerclass (& quot; myObject", myObject)
if registerobject and registerclass are called multiple times to be used by objects with the same key value, they will be updated by the latest assigned object
in JSON, you can also use global bridge in singleton mode to export all instance methods for all HTTP clients. It can be used to register factory class, but we should pay attention to authentication and security when using it. When used, the identifier is as follows: jsonrpcbridge. Getglobalbridge(). Registerobject (& quot; myObject", myObject); As above, it can also export all static methods
the most important part of this protocol is servlet. Now let's briefly introce it:
in this protocol, jsonrpcservelet acts as a transmitter to process the JSON transmitted over HTTP
6. 服务端示例:
<?php
namespace Rpc\Controller;
use Think\Controller\JsonRpcController;
class JsonApiController extends JsonRpcController
{
public function index(){
return 'Hello, JsonRPC!';
}
// 支持参数传入
public function test($name=''){
return "Hello, {$name}!";
}
}
客户端示例:
vendor('jsonRPC.jsonRPCClient');
$client = new \jsonRPCClient('http://www.tp.cn/index.php/Rpc/JsonApi');
$result = $client->index();
var_mp($result); // 结果:Hello, JsonRPC!
$result = $client->test('deeka');
var_mp($result); // 结果:Hello, deeka!
<?php
namespace Rpc\Controller;
use Think\Controller\JsonRpcController;
class JsonApiController extends JsonRpcController
{
public function index(){
return 'Hello, JsonRPC!';
}
// 支持参数传入
public function test($name=''){
return "Hello, {$name}!";
}
}
客户端示例:
vendor('jsonRPC.jsonRPCClient');
$client = new \jsonRPCClient('http://www.tp.cn/index.php/Rpc/JsonApi');
$result = $client->index();
var_mp($result); // 结果:Hello, JsonRPC!
$result = $client->test('deeka');
var_mp($result); // 结果:Hello, deeka!
7. I'm not God
8. You have to find daily tasks in daily tasks, and you need to complete other main tasks. You can't do it all the way according to tasks. You can't do it without the central task of floor cabinet. You won't be promoted to level 90 in Grundy, will you?
Hot content