Demo of Android send get and POST request _android

Source: Internet
Author: User
Tags finally block flush readline
4.0 after network access must be a separate child thread access, or can not run, there is a request to send the tool class Getpostutil
Copy Code code as follows:

public class Getpostutil
{
/**
* Request to send the Get method to the specified URL
*
* @param URL
* Send the requested URL
* @param params
* Request parameter, request parameter should be the form of name1=value1&name2=value2.
* @return The response of the remote resource represented by the URL
*/
public static string Sendget (string url, string params)
{
String result = "";
BufferedReader in = null;
Try
{
String urlname = URL + "?" + params;
URL realurl = new URL (urlname);
Opening and linking to URLs
URLConnection conn = Realurl.openconnection ();
To set common request properties
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 ();
Get all response header fields
map<string, list<string>> map = Conn.getheaderfields ();
Iterate through all the response header fields
For (String Key:map.keySet ())
{
SYSTEM.OUT.PRINTLN (key + "--->" + map.get (key));
}
Defines the response of a bufferedreader input stream to read a URL
in = new BufferedReader (
New InputStreamReader (Conn.getinputstream ()));
String Line;
while (line = In.readline ())!= null)
{
result = "\ n" + line;
}
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Send GET request exception!") "+ 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;
}
/**
* Request to send post method to specified URL
*
* @param URL
* Send the requested URL
* @param params
* Request parameter, request parameter should be the form of name1=value1&name2=value2.
* @return The response of the remote resource represented by the URL
*/
public static string Sendpost (string url, string params)
{
PrintWriter out = null;
BufferedReader in = null;
String result = "";
Try
{
URL realurl = new URL (URL);
Opening and linking to URLs
URLConnection conn = Realurl.openconnection ();
To set common request properties
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Send POST request must be set as follows two lines
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
Gets the output stream corresponding to the URLConnection object
out = new PrintWriter (Conn.getoutputstream ());
Send Request parameters
Out.print (params);
Buffer of flush output stream
Out.flush ();
Defines the response of a bufferedreader input stream to read a URL
in = new BufferedReader (
New InputStreamReader (Conn.getinputstream ()));
String Line;
while (line = In.readline ())!= null)
{
result = "\ n" + line;
}
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Send POST request exception!) "+ e);
E.printstacktrace ();
}
Use the finally block to turn off the output stream, the input stream
Finally
{
Try
{
if (out!= null)
{
Out.close ();
}
if (in!= null)
{
In.close ();
}
}
catch (IOException ex)
{
Ex.printstacktrace ();
}
}
return result;
}
}

Activity Class Code
Copy Code code 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);
Update UI with Handler
Final Handler h = new Handler () {
@Override
public void Handlemessage (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);
}
}
Related Article

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.