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数据:
服务端返回json结果:
Rest 接口例子
EntityUser实体类,类成员变量名就和返回json中的key一一对应
使用该接口
@RestService
ApiUser apiUser;
由于这只是项目中一部分,就不提供没有项目程序包下载了,有什么问题可以留言.
第一次写技术博客,如有不当,还请见谅.如果有什么问题/建议,欢迎留言说明.废话不说,直接上题.
题说:如何用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()); }
由于这只是项目中一部分,就不提供没有项目程序包下载了,有什么问题可以留言.
相关文章推荐
- Android 如何用HttpClient 以Get方式获取数据并添加http头信息
- android HttpGet 另开线程获取网络数据问题
- java或者android通过http获取服务端数据
- Android使用HTTP GET、HTTP POST获取网络数据
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- 【转载】Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据,作者:Jaiky_杰哥
- [Android]Http通信,HttpService服务端通过getEntity获取HttpClient客户端Post上来的数据
- Android建立网络连接,利用JSON数据获取百度图片搜索结果及GSON的简单使用
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- Android——使用Http的get方式获取数据
- 加上FastJsonHttpMessageConverter之后,前端获取不到数据
- android http数据通信 通过GET 请求获取数据
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android向EasyN摄像头发送http get请求获取媒体数据响应包
- android 客户端与服务端的通信 发送get和post请求并获取数据
- 【Android】使用http的Post和Get方式获取网络数据
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)