android jsonrpc 使用实例

169 篇文章 4 订阅
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();
		}
	}

	
}

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值