After 4.0, the network access must be accessed by a separate sub-thread; otherwise, it cannot be run. Here there is a request tool class GetPostUtil.
Copy codeThe Code is as follows: public class GetPostUtil
{
/**
* Send a GET request to a specified URL
*
* @ Param url
* Request URL
* @ Param params
* Request parameters. 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 );
// Open the connection with the URL
URLConnection conn = realUrl. openConnection ();
// Set common request attributes
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 all response header fields
Map <String, List <String> map = conn. getHeaderFields ();
// Traverse all response header fields
For (String key: map. keySet ())
{
System. out. println (key + "--->" + map. get (key ));
}
// Define 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 finally blocks 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
* Request URL
* @ Param params
* Request parameters. 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 );
// Open the connection with the URL
URLConnection conn = realUrl. openConnection ();
// Set common request attributes
Conn. setRequestProperty ("accept ","*/*");
Conn. setRequestProperty ("connection", "Keep-Alive ");
Conn. setRequestProperty ("user-agent ",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )");
// You must set the following two rows to send a POST request:
Conn. setDoOutput (true );
Conn. setDoInput (true );
// Obtain the output stream corresponding to the URLConnection object
Out = new PrintWriter (conn. getOutputStream ());
// Send Request Parameters
Out. print (params );
// Flush the buffer of the output stream
Out. flush ();
// Define 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 the POST request! "+ E );
E. printStackTrace ();
}
// Use finally blocks to close output streams and input streams
Finally
{
Try
{
If (out! = Null)
{
Out. close ();
}
If (in! = Null)
{
In. close ();
}
}
Catch (IOException ex)
{
Ex. printStackTrace ();
}
}
Return result;
}
}
Activity Code
Copy codeThe Code is as follows: public class GetPostMain extends Activity
{
Button get, post;
EditText show;
@ 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 = (EditText) findViewById (R. id. show );
// Use Handler to update the UI
Final Handler h = new Handler (){
@ Override
Public void handleMessage (Message msg ){
If (msg. what = 0x123 ){
Show. setText (msg. obj. toString ());
}
}
};
Get. setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
New Thread (new AccessNetwork ("GET", "http: // 192.168.1.88: 8080/abc/a. jsp", null, h). start ();
}
});
Post. setOnClickListener (new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
New Thread (new AccessNetwork ("POST", "http: // 192.168.1.88: 8080/abc/login. jsp "," name = crazyit.org & pass = leegang ", h )). start ();
}
});
}
}
Class AccessNetwork implements Runnable {
Private String op;
Private String url;
Private String params;
Private Handler h;
Public AccessNetwork (String op, String url, String params, Handler h ){
Super ();
This. op = op;
This. url = url;
This. params = params;
This. h = h;
}
@ Override
Public void run (){
Message m = new Message ();
M. what = 0x123;
If (op. equals ("GET ")){
Log. I ("iiiiiii", "Send GET request ");
M. obj = GetPostUtil. sendGet (url, params );
Log. I ("iiiiiii", ">>>>>>>>>>>>" + m. obj );
}
If (op. equals ("POST ")){
Log. I ("iiiiiii", "Send POST request ");
M. obj = GetPostUtil. sendPost (url, params );
Log. I ("gggggggg", ">>>>>>>>>>>>" + m. obj );
}
H. sendMessage (m );
}
}