Handler Handler = new Handler (new Android.os.Handler.Callback () {
@Override
public boolean handlemessage (Message msg) {
Switch (MSG.ARG1) {
Case 1:
Manipulating the acquired data
Break
Case 2:
Manipulating access to data
Break
Default
Break
}
return false;
}
});
Before networking, check the operation of the network is omitted to write.
public void Sendwebrequest () {
New Thread (New Runnable () {
@Override
public void Run () {
HttpURLConnection conn = null;
try {
URL url = new URL ("www.baidu.com");
conn = (httpurlconnection) url.openconnection ();
Conn.setrequestmethod ("get");//Get request mode: Read data from server
/*
* Post: Submits the data to the server. Note: Each data exists in the form of a key-value pair of data between & separated
* Conn.setrequestmethod ("POST"); Post: Submit data to the server
* DataOutputStream dataoutputstream=new DataOutputStream (Conn.getoutputstream ());
* Dataoutputstream.writebytes ("username=haha&password=123456"); Each data is in the form of a key-value pair, and the data is separated by &
*/
conn.setconnecttimeout (8 * 1000);
conn.setreadtimeout (1000 * 8);
conn.connect ();
bufferedreader reader = new BufferedReader (
new InputStreamReader (Conn.getinputstream ()));
stringbuffer buffer = new StringBuffer ();//for storing acquired data
string Line;
while (line = Reader.readline ()) = null) {
buffer.append (line);
}
//Note: Child threads cannot manipulate UI controls and need to go to the main thread (that is, the UI thread) to manipulate UI controls
Looper.prepare ();
message Message = Handler.obtainmessage ();
message.obj = buffer;
message.arg1 = 1;
handler.sendmessage (message);
looper.loop ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
IF (conn! = null) {
Conn.disconnect ();
}
}
}
}). Start ();
}
public void Sendhttpconnet () {
New Thread (New Runnable () {
public void Run () {
try {
HttpClient is an interface that cannot be new to itself. So new is a default httpclient
HttpClient HttpClient = (HttpClient) new Defaulthttpclient ();
HttpGet get = new HttpGet ("http://www.baidu.com"); GET request
/*
* POST request: Submit data to server, or upload file to server HttpPost post=new
* HttpPost ("http://www.baidu.com"); List<namevaluepair>
* Parameters=new arraylist<namevaluepair> (); Used to store data
* Parameters.Add (New Basicnamevaluepair ("name", "admin"));
* Parameters.Add (New Basicnamevaluepair ("pwd", "123456"));
* urlencodedformentity entity1=new urlencodedformentity (Parameters, "Utf-8");
* Post.setentity (ENTITY1);
*/
HttpResponse respone = Httpclient.execute (get);
Determine if the request is successful
if (Respone.getstatusline (). Getstatuscode () = = 200) {
httpentity entity = respone.getentity ();
String s = entityutils.tostring (entity, "utf-8"); Convert to String type
Looper.prepare ();
Message message = Handler.obtainmessage ();
Message.obj = s;
MESSAGE.ARG1 = 2;
Handler.sendmessage (message);
Looper.loop ();
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}). Start ();
}
Common HTTP requests from Android