編輯:關於Android編程
開篇
之前的一篇博客:Androidannotation使用之@Rest獲取資源及用戶登錄驗證(一):http://blog.csdn.net/nupt123456789/article/details/24384713 主要寫了Rest在用戶登錄的時候,需要JSESSION字段的問題。本博客主要寫JSON格式的轉換。
@Rest的參考文檔:
https://github.com/excilys/androidannotations/wiki/Rest-API#rest
簡介:
從上一篇博客中,我們可以看出,我們直接再浏覽器中請求http://192.168.0.101:8080/cbvr/getUserInfoList.action 的時候,返回的字符串其實是JSON格式。我們上一篇博客,就是把它直接當String進行處理了,沒有出現什麼問題。當然,我們接下來,可以使用GSON對String進行解析,這沒有什麼問題。然而,我們通常想,我們換一個轉換器不就行了嗎?代碼如下:
/*
* $filename: UserService.java,v $
* $Date: 2014-4-20 $
* Copyright (C) ZhengHaibo, Inc. All rights reserved.
* This software is Made by Zhenghaibo.
*/
package com.example.testaa;
import org.androidannotations.annotations.rest.Accept;
import org.androidannotations.annotations.rest.Post;
import org.androidannotations.annotations.rest.Rest;
import org.androidannotations.api.rest.MediaType;
import org.androidannotations.api.rest.RestClientErrorHandling;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
/*
*@author: ZhengHaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2014-4-20 Nanjing,njupt,China
*/
@Rest(rootUrl = "http://192.168.0.101:8080/cbvr", converters = {GsonHttpMessageConverter.class})
public interface UserService extends RestClientErrorHandling{
@Post("/getUserInfoList.action")
@Accept(MediaType.APPLICATION_JSON)
ResponseEntity getUserInfoList();
}
05-02 16:58:32.644: W/System.err(7454): org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.example.testaa.DataGrid] and content type [text/html;charset=UTF-8]
response.setContentType("text/html;charset=UTF-8");response.setContentType("application/json;charset=utf-8");package com.example.testaa;
/*
*@author: ZhengHaibo
*web: http://blog.csdn.net/nuptboyzhb
*GitHub https://github.com/nuptboyzhb
*mail: zhb931706659@126.com
*2014-1-12 Nanjing,njupt,China
*/
public class Userinfo {
/**
* {field : 'yhm',title : '用戶名',width : 150},
{field : 'pwd',title : '密碼',width : 150},
{field : 'yhqx',title : '用戶權限',width : 150},
{field : 'zcsj',title : '注冊時間',width : 150},
{field : 'bz',title : '備注',width : 180}] ];
*/
private String id;
private String yhm;
private String pwd;
private String yhqx;
private String zcsj;
private String bz;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getYhm() {
return yhm;
}
public void setYhm(String yhm) {
this.yhm = yhm;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getYhqx() {
return yhqx;
}
public void setYhqx(String yhqx) {
this.yhqx = yhqx;
}
public String getZcsj() {
return zcsj;
}
public void setZcsj(String zcsj) {
this.zcsj = zcsj;
}
public String getBz() {
return bz;
}
public void setBz(String bz) {
this.bz = bz;
}
}/*
* $filename: DataGrid.java,v $
* $Date: 2013-10-11 $
* Copyright (C) ZhengHaibo, Inc. All rights reserved.
* This software is Made by Zhenghaibo.
*/
package com.example.testaa;
/*
*@author: ZhengHaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2013-10-11 Nanjing,njupt,China
*/
public class DataGrid{
private int total;
private Userinfo[] rows;
public Userinfo[] getRows() {
return rows;
}
public void setRows(Userinfo[] rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public DataGrid(int total, Userinfo[] rows) {
this.total = total;
this.rows = rows;
}
public DataGrid( ) {
}
public void setTotal(int total) {
this.total = total;
}
}
/*
* $filename: ErrorHandlerForUserService.java,v $
* $Date: 2014-4-29 $
* Copyright (C) ZhengHaibo, Inc. All rights reserved.
* This software is Made by Zhenghaibo.
*/
package com.example.testaa;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.api.rest.RestErrorHandler;
import org.springframework.web.client.RestClientException;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
/*
*@author: ZhengHaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2014-4-29 Nanjing,njupt,China
*/
@EBean
public class MyErrorHandler implements RestErrorHandler {
@RootContext
Context context;
@Override
public void onRestClientExceptionThrown(RestClientException e) {
// TODO Auto-generated method stub
e.printStackTrace();
Log.d("REST", e.toString());
showError();
}
@UiThread
void showError(){
Toast.makeText(context, "Rest Error...", Toast.LENGTH_SHORT).show();
}
}
剩下的就是MainActivity
package com.example.testaa;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.springframework.http.ResponseEntity;
import android.app.Activity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
/*
*@author: ZhengHaibo
*web: http://blog.csdn.net/nuptboyzhb
*mail: zhb931706659@126.com
*2014-4-15 Nanjing,njupt,China
*/
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
private static final String TAG="AAREST";
@ViewById
Button getUser;
@ViewById
TextView myTextView;
@RestService
UserService userService;
@Bean
MyErrorHandler errorHandlerForUserService;
@AfterViews
void afterView(){
//設置ErrorHandler
userService.setRestErrorHandler(errorHandlerForUserService);
}
/**
* 獲取用戶列表
*/
@Click
void getUser() {
getUserInBackground();
}
/**
* 獲取用戶列表
* 無需登錄
*/
@Background
void getUserInBackground(){
//String result = userService.getUserInfoList();
//Gson gson = new Gson();
//DataGrid dataGrid = gson.fromJson(result, DataGrid.class);
ResponseEntity responseEntiy = userService.getUserInfoList();
if(null == responseEntiy){
return;
}
DataGrid dataGrid = responseEntiy.getBody();
Userinfo[] userinfos= dataGrid.getRows();
String string = "";
for(Userinfo userinfo:userinfos){
string = string + "user:"+ userinfo.getYhm();
Log.d(TAG, userinfo.getYhm());
}
Log.d(TAG, string);
displayTextView(string);
}
@UiThread
void displayTextView(String string){
myTextView.setText(string);
}
}
總結:
整個項目使用AndroidAnnotation框架。本次博客主要解決服務端和android進行json交互的情況。
缺點:Response的setContentType設置修改後,可能影響原網站對浏覽器的支持,因此,需要根據不同場景進行選擇。
整個項目下載地址:http://download.csdn.net/detail/nuptboyzhb/7283863
未經允許,不得用於商業目的
Android開發入門之Service用法分析
本文實例講述了Android中Service用法。分享給大家供大家參考,具體如下:關於Service的講解網上已經很多了,這裡是關於自己通過寫代碼Service的一點體會
android學習三(Intent)
前面的博文介紹了Activity,可能學的還不太爽哦,所以接下來學習android的Intent,它是android的信使,可以進行各個組件之間進行通信了。我依然在前面的
Android自學筆記-15-Activity的生命周期
很長時間沒有寫博客了,懶了,感慨一下。 Activity的生命周期主要就是一張下面的圖: 下面通過代碼簡單的介紹一下,具體的一些內容看代碼的注釋: package c
cocos - js (v3.12) 搭建技術文章
下載必備的軟件包下載並安裝WebStorm7。WebStorm7目前的穩定版本是7.0.3。為什麼我們選擇WebStorm?因為它提供了許多功能,如JavaScript代