There are two ways of submitting data to the server, post and get. The difference between the two is mainly three points, security, length limit, data structure. Where GET request security is relatively poor, the data length is limited by the browser address bar and no method body is used. Both are more important ways of submitting data. Here is a brief introduction to three types of post and get submissions. Either way to implement post and get,get access paths to carry data, and post submission is to put the data in the method body.
Common method Implementation Get/post submission:
Strictly follow the HTTP protocol for data transmission. In an Android development environment, because the main thread cannot be accessed by the network, it is necessary to initiate a child thread to submit data to the server. For more intuitive observation data, you can display server feedback on the program screen. And because the child thread cannot change the UI interface, the Hnndler agent needs to be introduced. The basic step is to get the URL path, get the HTTP connection according to the path, use the HttpURLConnection object to set the relevant HTTP configuration information, submit the way and get the feedback code for the get/post. When the response code is 200, the commit is successful, and the feedback can be obtained by HttpURLConnection as a stream.
Ordinary GRT submission Method:
public void load (view view) {final String QQ = Et_qq.gettext (). toString (). Trim ();
Final String pwd = Et_pwd.gettext (). toString (). Trim (); if (Textutils.isempty (QQ) | |
Textutils.isempty (pwd)) {Toast.maketext (mainactivity.this, "QQ number or password is empty", 0). Show ();
Return
Final String Path = "http://192.168.1.114:8080/qqload/qqload?qq=" + QQ + "&pwd=" + pwd;
New Thread () {public void run () {try {URL url = new URL (path);
HttpURLConnection conn = (httpurlconnection) URL. OpenConnection ();
Conn.setrequestmethod ("get");
Conn.setreadtimeout (5000);
int code = Conn.getresponsecode ();
if (code = =) {InputStream is = Conn.getinputstream ();
String result = Streamtools.readstream (IS);
Message msg = Message.obtain ();
Msg.what = SUCCESS;
Msg.obj = result;
Handler.sendmessage (msg);
else {Message msg = Message.obtain ();
Msg.what = ERROR1;
Handler.sendmessage (msg); }
} catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
Message msg = Message.obtain ();
Msg.what = ERROR2;
Handler.sendmessage (msg);
}}.start (); }
normal post submission:
public void load (view view) {final String QQ = Et_qq.gettext (). toString (). Trim ();
Final String pwd = Et_pwd.gettext (). toString (). Trim (); if (Textutils.isempty (QQ) | |
Textutils.isempty (pwd)) {Toast.maketext (mainactivity.this, "QQ number or password is empty", 0). Show ();
Return
Final String Path = "Http://192.168.1.114:8080/qqload/qqload";
New Thread () {public void run () {try {URL url = new URL (path);
HttpURLConnection conn = (httpurlconnection) URL. OpenConnection ();
Conn.setrequestmethod ("POST");
Conn.setreadtimeout (5000);
Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
String data = "qq=" +urlencoder.encode (QQ, "utf-8") + "&pwd=" + urlencoder.encode (pwd, "utf-8");
Conn.setrequestproperty ("Content-length", String.valueof (Data.length ()));
Conn.setdooutput (TRUE);
Conn.getoutputstream (). Write (Data.getbytes ());
int code = Conn.getresponsecode (); if (code = =) {InputStream is = CoNn.getinputstream ();
String result = Streamtools.readstream (IS);
Message msg = Message.obtain ();
Msg.what = SUCCESS;
Msg.obj = result;
Handler.sendmessage (msg);
else {Message msg = Message.obtain ();
Msg.what = ERROR1;
Handler.sendmessage (msg);
The catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
Message msg = Message.obtain ();
Msg.what = ERROR2;
Handler.sendmessage (msg);
}}.start (); }
Using Httpclien to implement Get/post submission requires only a few steps:
1. Create a HttpClient object to enable the browser to open the function
HttpClient client = new DefaultHttpClient();
2. Enter address or data, use HttpGet () or HttpPost (), pass in the path to access, get HttpGet or HttpPost object.
HttpGet httpGet = new HttpGet(path);
3. Send the HttpGet or HttpPost object to the server, realize the function of knocking carriage, get HttpResponse object.
HttpResponse response = client.execute(httpGet);
4. Get the HttpResponse object to obtain the status code in the status line, and determine the status Code status code.
int code = response.getStatusLine().getStatusCode();
5. Also use the HttpResponse object to obtain the corresponding content, into the stream object. Finally, the resulting stream object is converted to a string for display.
InputStream is = response.getEntity().getContent();
One thing to note is that when you use a POST request to pass a value, you need one more step. Specifically, you first create a list collection, which is represented by Namevaluepair, which stores the data to be passed, similar to a key-value pair. Next, add the data to the collection to submit. Finally, the collection is stored in the request body with the HttpPost object.
Implement get commit with HttpClient:
public void load (view view) {final String QQ = Et_qq.gettext (). toString (). Trim ();
Final String pwd = Et_pwd.gettext (). toString (). Trim (); if (Textutils.isempty (QQ) | |
Textutils.isempty (pwd)) {Toast.maketext (mainactivity.this, "QQ number or password is empty", 0). Show ();
Return
Final String Path = "http://192.168.1.114:8080/qqload/qqload?qq=" + QQ + "&pwd=" + pwd;
New Thread () {public void run () {try {httpclient client = new Defaulthttpclient ();
HttpGet httpget = new HttpGet (path);
HttpResponse response = Client.execute (HttpGet);
int code = Response.getstatusline (). Getstatuscode ();
if (code = =) {InputStream is = response.getentity (). getcontent ();
String result = Streamtools.readstream (IS);
Message msg = Message.obtain ();
Msg.what = SUCCESS;
Msg.obj = result;
Handler.sendmessage (msg);
else {Message msg = Message.obtain ();
Msg.what = ERROR1;
Handler.sendmessage (msg); }
}catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
Message msg = Message.obtain ();
Msg.what = ERROR2;
Handler.sendmessage (msg);
}}.start (); }
to implement post submission with HttpClient:
public void load (view view) {final String QQ = Et_qq.gettext (). toString (). Trim ();
Final String pwd = Et_pwd.gettext (). toString (). Trim (); if (Textutils.isempty (QQ) | |
Textutils.isempty (pwd)) {Toast.maketext (mainactivity.this, "QQ number or password is empty", 0). Show ();
Return
Final String Path = "Http://192.168.1.114:8080/qqload/qqload";
New Thread () {public void run () {try {httpclient client = new Defaulthttpclient ();
HttpPost HttpPost = new HttpPost (path);
list<namevaluepair> parameter = new ArrayList ();
Parameter.add (New Basicnamevaluepair ("QQ", QQ));
Parameter.add (New Basicnamevaluepair ("pwd", pwd));
Httppost.setentity (new urlencodedformentity (parameter, "Utf-8"));
HttpResponse response = Client.execute (HttpPost);
int code = Response.getstatusline (). Getstatuscode ();
if (code = =) {InputStream is = response.getentity (). getcontent ();
String result = Streamtools.readstream (IS); Message msg = MESSAGE.OBTain ();
Msg.what = SUCCESS;
Msg.obj = result;
Handler.sendmessage (msg);
else {Message msg = Message.obtain ();
Msg.what = ERROR1;
Handler.sendmessage (msg);
The catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
Message msg = Message.obtain ();
Msg.what = ERROR2;
Handler.sendmessage (msg);
}}.start (); }
Implement Get/post submission using open source framework:
Using the framework to implement Get/post commits does not require the child thread to be reopened. Directly in the main thread to carry out get/post, greatly reducing the workload. However, you need to guide the package before operation. It then creates a Asynchttpclient object, sends the request using the Post method of the Asynchttpclient object and the Get method, and obtains the appropriate information in the Asynchttpresponsehandler () object. Also, if you post a request, you still need to pass the value. Here you can add the value you want to pass with the Requestparams object.
To add a jar package under a file:
Implement get commit with open source framework:
public void load (view view) {
final String qq = Et_qq.gettext (). toString (). Trim ();
Final String pwd = Et_pwd.gettext (). toString (). Trim ();
if (Textutils.isempty (QQ) | | Textutils.isempty (pwd)) {
Toast.maketext (mainactivity.this, "QQ number or password is empty", 0). Show ();
return;
}
Final String Path = "http://192.168.1.114:8080/qqload/qqload?qq=" + QQ +
"&pwd=" + pwd;
Asynchttpclient client = new Asynchttpclient ();
Client.get (Path, new Asynchttpresponsehandler () {
@Override public
void onsuccess (int statusCode, header[] Headers, byte[] responsebody) {
//TODO auto-generated method Stub
Tv_result.settext (New String (responsebody ));
}
@Override public
void onfailure (int statusCode, header[] headers,
byte[] responsebody, throwable error) {
//TODO auto-generated method stub
tv_result.settext ("Error Reason:" + new String (responsebody);
}
);
}
To implement a POST request with an open source framework:
public void load (view view) {final String QQ = Et_qq.gettext (). toString (). Trim ();
Final String pwd = Et_pwd.gettext (). toString (). Trim (); if (Textutils.isempty (QQ) | |
Textutils.isempty (pwd)) {Toast.maketext (mainactivity.this, "QQ number or password is empty", 0). Show ();
Return
Final String Path = "Http://192.168.1.114:8080/qqload/qqload";
Asynchttpclient client = new Asynchttpclient ();
Requestparams params = new Requestparams ();
Params.add ("QQ", QQ);
Params.add ("pwd", PWD); Client.post (path,params,new Asynchttpresponsehandler () {@Override public void onsuccess (int statusCode, header[
] headers, byte[] responsebody) {//TODO auto-generated Method Stub tv_result.settext (new String (responsebody)); @Override public void onfailure (int statusCode, header[] headers, byte[] responsebody, throwable error
) {//TODO auto-generated Method Stub tv_result.settext (new String (responsebody));
}
}); }
The function that can be realized in any one of these ways is to submit data from the Android handset side to the server side, the server side to judge, and return the corresponding results. Three kinds of ways have advantages and disadvantages, the implementation of the same effect, in the actual use of the process can be based on their own needs to choose.
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!