您的位置:首页 > 理论基础 > 计算机网络

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

2014-08-09 12:32 363 查看
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());
}




由于这只是项目中一部分,就不提供没有项目程序包下载了,有什么问题可以留言.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐