android annotations Rest Get GsonHttpMessageConverter 获取服务端简单数据

2014年暑假(-)

第一次写技术博客,如有不当,还请见谅.如果有什么问题/建议,欢迎留言说明.废话不说,直接上题.

题说:如何用android annotations rest与服务端进行简单交互.

Android annotations Rest  官方 Api :https://github.com/excilys/androidannotations/wiki/Rest-API#rest


首先,说Rest Get的使用(Post也类似)

简单服务端php接受与反回json数据:

		header("Content-Type:application/json;charset=utf-8");
		if($_GET["userAccount"]=="saby"){
				$data=array(
 				'states'=>'succeed',
 				'userName'=>'健女',
 				'userLogo'=>'http://img3.douban.com/icon/ul67733236-1.jpg',
 				'userBirthday'=>'1994-8-3',
 				'userHeight'=>162,
 				'userWeight'=>47,
 				'userCity'=>'茂名',
 				'userQQ'=>'12345566',
 				'userWeibo'=>'Lights',
 				'userPhone'=>'110'
 				);
 					
	 	}else {
	 		$data=array('states'=>'invalid');
	 	}


服务端返回json结果:

{
    "states": "succeed",
    "userName": "健女",
    "userLogo": "http://img3.douban.com/icon/ul67733236-1.jpg",
    "userBirthday": "1994-8-3",
    "userHeight": 162,
    "userWeight": 47,
    "userCity": "茂名",
    "userQQ": "12345566",
    "userWeibo": "Lights",
    "userPhone": "110"
}


 Rest 接口例子 

package com.xxx.api;

import org.androidannotations.annotations.rest.Post;
import org.androidannotations.annotations.rest.Rest;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.client.RestClientException;

import com.xxx.dao.EntityUser;
/**
 * 
* Title: ApiUser 
* Description: 
* Company: Macroyep
* @author Macro
* @date 2014年8月9日
 */
@Rest(rootUrl = "http://192.168.1.189/~apple/php/ClouthX/index.php/Home/index/", converters = { GsonHttpMessageConverter.class })
public interface ApiUser {

	@Post("loginByAccount?userAccount={userAccount}")
	EntityUser getUserEntity(String userAccount) throws RestClientException;
}


EntityUser实体类,类成员变量名就和返回json中的key一一对应

package com.xxx.dao;


public class EntityUser {
	private String states;
	private String userPwd;
	private String userAccount;
	private String userName;
	private String userLogo;
	private String userBirthday;
	private int userHeight;
	private int userWeight;
	private String userCity;
	private String userQQ;
	private String userWeibo;
	private String userPhone;

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getStates() {
		return states;
	}

	public void setStates(String states) {
		this.states = states;
	}

	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserLogo() {
		return userLogo;
	}

	public void setUserLogo(String userLogo) {
		this.userLogo = userLogo;
	}

	public String getUserBirthday() {
		return userBirthday;
	}

	public void setUserBirthday(String userBirthday) {
		this.userBirthday = userBirthday;
	}

	public int getUserHeight() {
		return userHeight;
	}

	public void setUserHeight(int userHeight) {
		this.userHeight = userHeight;
	}

	public int getUserWeight() {
		return userWeight;
	}

	public void setUserWeight(int userWeight) {
		this.userWeight = userWeight;
	}

	public String getUserCity() {
		return userCity;
	}

	public void setUserCity(String userCity) {
		this.userCity = userCity;
	}

	public String getUserQQ() {
		return userQQ;
	}

	public void setUserQQ(String userQQ) {
		this.userQQ = userQQ;
	}

	public String getUserWeibo() {
		return userWeibo;
	}

	public void setUserWeibo(String userWeibo) {
		this.userWeibo = userWeibo;
	}

	public String getUserPhone() {
		return userPhone;
	}

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}



使用该接口

@RestService

ApiUser apiUser;

	// 后台处理
	@Background
	void getUser() {
		try {
			EntityUser entity = apiUser.getUserEntity("saby");
			if (entity.getStates().equals("succeed"))
				showResult(entity);
		} catch (RestClientException e) {
			// 处理异常
		}
	}

	// ui线程处理
	@UiThread
	void showResults(EntityUser entity) {
		Log.i("用户名字:", entity.getUserName());
	}


 




由于这只是项目中一部分,就不提供没有项目程序包下载了,有什么问题可以留言.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值