golang作为server向android提供数据服务
中间交换的数据是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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。