golang作为server向android提供数据服务

coolyylu · · 2466 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

中间交换的数据是json ,后台数据库服务器是sqlserver2012

android通过post或者get方式访问

如get方式http://192.168.255.13:7080/tblFileList?tblFileList2_Title_search1=t01

  1 package main
  2 
  3 import (    
  4     "time"
  5     "log"
  6     "net/http"    
  7     "os"
  8     _ "code.google.com/p/odbc"
  9     "database/sql"    
 10     "fmt"
 11     "encoding/json"
 12 )
 13 
 14 //http://192.168.255.13:7080/tblFileList?tblFileList2_Title_search1=t01
 15 
 16 func get_tblFileList(w http.ResponseWriter, r *http.Request) {
 17 
 18     log.Print(r.RequestURI)    
 19     tblFileList2_Title_search1 := r.FormValue("tblFileList2_Title_search1")
 20     tblFileList2_Title_search2 := r.FormValue("tblFileList2_Title_search2")
 21     
 22     fmt.Println("参数:",tblFileList2_Title_search1,tblFileList2_Title_search2)
 23     list:= getData(tblFileList2_Title_search1,tblFileList2_Title_search2)
 24     body, err := json.Marshal(list)
 25     if err == nil{
 26         w.Write(body)
 27     }else{
 28         w.Write([]byte("error"))
 29     }
 30     
 31 }
 32 
 33 
 34 func login(w http.ResponseWriter, r *http.Request) {
 35     w.Write([]byte("token=123"))
 36 }
 37 func main() {
 38 
 39     http.HandleFunc("/tblFileList", get_tblFileList)
 40     http.HandleFunc("/login", login)
 41 
 42     log.Println("Start serving on port 7080")
 43     
 44 
 45     err := http.ListenAndServe(":7080", nil)
 46     if err != nil{
 47         fmt.Println(err)
 48     }
 49     
 50     os.Exit(0)
 51 }
 52 
 53 func getData(filter1 string ,filter2 string) []tblFileList {
 54     conn, err := sql.Open("odbc", "driver={SQL Server Native Client 11.0};server=test;database=db1;uid=sa;pwd=sa1234;")
 55     if err != nil {
 56         fmt.Println("Connecting Error")
 57         return nil
 58     }
 59     defer conn.Close()
 60     filter_where :="1=1 "
 61     if filter1!=""{
 62         filter_where += " and title like '" + filter1 + "%'"
 63     } 
 64     if filter2!=""{
 65         filter_where += " and title like '%" + filter2 + "%'"
 66     } 
 67 
 68     zsql :=`select 
 69             Title,
 70             ReadCount,
 71             LastReadUser,
 72             isnull(LastReadTime,cast('1900-01-01' as datetime)) as LastReadTime  
 73             from tblFileList     where ` + filter_where
 74     log.Println(zsql)
 75     stmt, err := conn.Prepare(zsql)
 76     if err != nil {
 77         fmt.Println("Query Error", err)
 78         return nil
 79     }
 80     defer stmt.Close()
 81     row, err := stmt.Query()
 82     if err != nil {
 83         fmt.Println("Query Error", err)
 84         return nil
 85     }
 86     defer row.Close()
 87 
 88     rowlist :=[]tblFileList{}
 89     for row.Next() {
 90         var Title string
 91         var ReadCount int
 92         var LastReadUser string
 93         var LastReadTime time.Time        
 94         if err := row.Scan(&Title, &ReadCount, &LastReadUser, &LastReadTime); err == nil {
 95             //fmt.Println(Title, ReadCount,LastReadUser,LastReadTime)
 96             var detail tblFileList
 97 
 98             detail.Title  = Title
 99             detail.ReadCount = ReadCount
100             detail.LastReadUser = LastReadUser
101             detail.LastReadTime = LastReadTime            
102             if err != nil {
103                 panic(err.Error())
104             }
105             rowlist = append(rowlist,detail,)
106 
107         } else {
108             fmt.Println("data Error", err)
109         }
110     }
111     
112     fmt.Printf("%s\n", "finish" ,len(rowlist))
113     return rowlist;
114 }
115 
116 type tblFileList struct {
117     Title   string    `json:"Title"`
118     ReadCount  int   `json:"ReadCount"`
119     LastReadUser   string    `json:"LastReadUser"`
120     LastReadTime   time.Time    `json:"LastReadTime"`
121 }

 


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:coolyylu

查看原文:golang作为server向android提供数据服务

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2466 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传