package com.example.androidjsonrpc;
import java.util.List;
import org.alexd.jsonrpc.JSONRPCClient;
import org.alexd.jsonrpc.JSONRPCException;
import org.alexd.jsonrpc.JSONRPCParams.Versions;
import com.alibaba.fastjson.JSON;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void run(View view)
{
new Thread(new Runnable() {
@Override
public void run() {
runinthread();
}
}).start();
}
public void runinthread()
{
//https://code.google.com/p/android-json-rpc/downloads/list
//http://www.oschina.net/p/android-json-rpc
JSONRPCClient client = JSONRPCClient.create("http://10.0.0.107/json_server/server.php",Versions.VERSION_2);
client.setConnectionTimeout(2000);
client.setSoTimeout(2000);
try
{
String string = client.callString("fuck","android谷歌",15,32);
Log.i("androidjsonrpc", "fuck="+string);
int i = client.callInt("add", 56, 25);
Log.i("androidjsonrpc", i+"");
//Student student=(Student) client.call("getstudent", new Student(1,"name",123));
//Log.i("androidjsonrpc", student.toString());
// Log.i("androidjsonrpc", client.call("getstudent", new Student(1,"name",123)).toString());
// Log.i("androidjsonrpc", client.call("getstudents", "xx").toString());
String str=client.callString("getstudent", new Student(1,"name",123));
Log.i("androidjsonrpc", str);
//fastjson 转换json字符串为对象
Student student=JSON.parseObject(str, Student.class);
Log.i("androidjsonrpc", student.toString());
str=client.callString("getstudents", "xx");
Log.i("androidjsonrpc", str);
//使用到fastjson 转换json数组为list对象
List<Student> students=JSON.parseArray(str, Student.class);
Log.i("androidjsonrpc", students.toString());
}
catch (JSONRPCException e)
{
e.printStackTrace();
}
}
}
android jsonrpc 使用实例
最新推荐文章于 2024-08-18 10:30:50 发布