For many new users, it is still difficult to implement communication between the android client and the server. Here I provide a better class packageorg.crazyit.net; importjava. io. *; importjava.net. *; importjava. util. *; *** Description: * brsite: ahrefwww.crazyit.org
For many new users, it is still difficult to implement communication between the android client and the server. Here I provide a better class package org.crazyit.net; import java. io. *; import java.net. *; import java. util. *;/*** Description: * br/site: a href = "http://www.crazyit.org"
For many new usersImplementationCommunication between android and servers is still a problem. Here I provide a better class
Package org.crazyit.net; import java. io. *; import java.net. *; import java. util. *;/*** Description :*
Site: crazyit.org *
Copyright (C), 2001-2014, Yeeku. H. Lee *
This program is protected by copyright laws .*
Program Name :*
Date: * @ author Yeeku. h. lee kongyeeku@163.com * @ version 1.0 */public class GetPostUtil {/*** send a GET Method Request to a specified URL * @ param url send the request URL * @ param params request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return URL indicates the response of the Remote resource */public static String sendGet (String url, String params) {String result = ""; BufferedReader in = null; try {String urlName = url + "? "+ Params; URL realUrl = new URL (urlName); // enable the URL Connection URLConnection conn = realUrl. openConnection (); // set the common request attribute conn. setRequestProperty ("accept", "*/*"); conn. setRequestProperty ("connection", "Keep-Alive"); conn. setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // establish the actual connection conn. connect (); // ① // obtain the Map of all response header fields
> Map = conn. getHeaderFields (); // traverses all response header fields for (String key: map. keySet () {System. out. println (key + "--->" + map. get (key);} // defines the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (conn. getInputStream (); String line; while (line = in. readLine ())! = Null) {result + = "\ n" + line;} catch (Exception e) {System. out. println ("an Exception occurred when sending a GET request! "+ E); e. printStackTrace () ;}// use the finally block to close the input stream finally {try {if (in! = Null) {in. close () ;}} catch (IOException ex) {ex. printStackTrace () ;}} return result;}/*** send a POST method request to a specified URL * @ param url the request URL * @ param params request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return URL indicates the response of the Remote resource */public static String sendPost (String url, String params) {PrintWriter out = null; BufferedReader in = null; String result = ""; try {URL realUrl = new URL (url); // enable the URL Connection URLConnection conn = realUrl. openConnection (); // set the common request attribute conn. setRequestProperty ("accept", "*/*"); conn. setRequestProperty ("connection", "Keep-Alive"); conn. setRequestProperty ("user-agent", "M Ozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); // The following two rows of conn must be set to send a POST request. setDoOutput (true); conn. setDoInput (true); // get the output stream of the URLConnection object out = new PrintWriter (conn. getOutputStream (); // sends the request parameter out. print (params); // ② // flush the Buffer out of the output stream. flush (); // defines the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (conn. getInputStream (); String line; while (line = in. readL Ine ())! = Null) {result + = "\ n" + line;} catch (Exception e) {System. out. println ("an Exception occurred when sending a POST request! "+ E); e. printStackTrace () ;}// use finally blocks to close the output stream and input stream finally {try {if (out! = Null) {out. close () ;}if (in! = Null) {in. close () ;}catch (IOException ex) {ex. printStackTrace () ;}} return result ;}}
Here we will make a simple android Client
Here is the layout file in layout.
Here we will briefly write MainActivity
Package org.crazyit.net; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView;/*** Description :*
Site: crazyit.org *
Copyright (C), 2001-2014, Yeeku. H. Lee *
This program is protected by copyright laws .*
Program Name :*
Date: * @ author Yeeku. h. lee kongyeeku@163.com * @ version 1.0 */public class GetPostMain extends Activity {Button get, post; TextView show; // String response representing the server response; Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {if (msg. what = 0x123) {// set the show component to display the server response show. setText (response) ;}};@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); get = (Button) findViewById (R. id. get); post = (Button) findViewById (R. id. post); show = (TextView) findViewById (R. id. show); get. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {new Thread () {@ Override public void run () {response = GetPostUtil. sendGet (" http://192.168.0.102/test/index.php ", Null); // send a message to notify the UI thread to update the UI component handler. sendEmptyMessage (0x123 );}}. start () ;}}); post. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {new Thread () {@ Override public void run () {response = GetPostUtil. sendPost (" http://192.168.0.102/test/index.php "," Name = Shuai & pass = leegang ");}}. start (); // send a message to notify the UI thread to update the UI component handler. sendEmptyMessage (0x123 );}});}}
Php testing is simple, with only one page
For many new users, echo works like printf in C. In fact, echo is most important to return the results produced by the server.
Here is
After a post request is sent:
This simple description is complete.