Golang provides data services to Android as a server

Source: Internet
Author: User

The data in the middle Exchange is JSON, and the background database server is sqlserver2012

Android access via post or get mode

such as Get mode http://192.168.255.13:7080/tblFileList?tblFileList2_Title_search1=t01

1 Package Main2 3 Import (4"Time"5"Log"6"Net/http"7"OS"8_ "Code.google.com/p/odbc"9"Database/sql"Ten"FMT" One"Encoding/json" A ) -  - //http://192.168.255.13:7080/tblFileList?tblFileList2_Title_search1=t01 the  -Func get_tblfilelist (w http. Responsewriter, R *http. Request) { -  - log. Print (R.requesturi) +Tblfilelist2_title_search1: = R.formvalue ("Tblfilelist2_title_search1") -TBLFILELIST2_TITLE_SEARCH2: = R.formvalue ("Tblfilelist2_title_search2") +      AFmt. PRINTLN ("parameter:", TBLFILELIST2_TITLE_SEARCH1,TBLFILELIST2_TITLE_SEARCH2) atlist:=GetData (TBLFILELIST2_TITLE_SEARCH1,TBLFILELIST2_TITLE_SEARCH2) -Body, err: =JSON. Marshal (list) -     ifErr = =nil{ - W.write (body) -}Else{ -W.write ([]byte("Error")) in     } -      to } +  -  theFunc Login (w http. Responsewriter, R *http. Request) { *W.write ([]byte("Token=123")) $ }Panax Notoginseng Func Main () { -  thehttp. Handlefunc ("/tblfilelist", Get_tblfilelist) +http. Handlefunc ("/login", login) A  theLog. Println ("Start serving on port 7080") +      -  $ERR: = http. Listenandserve (": 7080", nil) $     ifErr! =nil{ - FMT. PRINTLN (ERR) -     } the      -Os. Exit (0)Wuyi } the  - func getData (Filter1 string, filter2 string) []tblfilelist { WuConn, err: = SQL. Open ("ODBC", "Driver={sql Server Native Client 11.0};server=test;database=db1;uid=sa;pwd=sa1234;")) -     ifErr! =Nil { AboutFmt. PRINTLN ("Connecting Error") $         returnNil -     } - defer Conn. Close () -Filter_where: = "1=1" A     ifFilter1!= ""{ +Filter_where + = "and title like" + Filter1 + "% '" the     }  -     ifFilter2!= ""{ $Filter_where + = "and title like '%" + filter2 + "% '" the     }  the  theZsql: =' Select the Title, - Readcount, in Lastreaduser, theIsNull (Lastreadtime,cast (' 1900-01-01 ')As datetime)) As Lastreadtime theFrom Tblfilelist where ' +Filter_where About log. Println (Zsql) thestmt, err: =Conn. Prepare (Zsql) the     ifErr! =Nil { theFmt. Println ("Query Error", Err) +         returnNil -     } the defer stmt. Close ()BayiRow, err: =stmt. Query () the     ifErr! =Nil { theFmt. Println ("Query Error", Err) -         returnNil -     } the defer row. Close () the  theRowlist: =[]tblfilelist{} the      forrow. Next () { -         varTitle String the         varReadcountint the         varLastreaduser String the         varlastreadtime time. time94         ifERR: = row. Scan (&title, &readcount, &lastreaduser, &lastreadtime); Err = =Nil { the             //FMT. Println (Title, Readcount,lastreaduser,lastreadtime) the             varDetail Tblfilelist the 98Detail. Title =Title AboutDetail. Readcount =Readcount -Detail. Lastreaduser =Lastreaduser101Detail. Lastreadtime =Lastreadtime102             ifErr! =Nil {103 Panic (err. Error ())104             } theRowlist =Append (Rowlist,detail,)106 107}Else {108Fmt. PRINTLN ("Data Error", Err)109         } the     }111      theFmt. Printf ("%s\n", "Finish", Len (rowlist))113     returnrowlist; the } the  the type tblfilelist struct {117Title string ' JSON: ' title '`118Readcountint' JSON: ' Readcount '`119Lastreaduser string ' JSON: ' Lastreaduser '` -Lastreadtime time. Time ' JSON: "Lastreadtime"`121}

Golang provides data services to Android as a server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.