Use get to submit data to server side under Android

Source: Internet
Author: User

To submit data from the client to the server side:

Use the get method to submit data to the server side, and put the parameter group behind the URL Address:

Http://192.168.0.10:8080/web/servlet/LoginServlet

? username=123&password=123445

Source Plate :

Package com.itheima.qqlogin;

Import Java.io.InputStream;

Import java.net.HttpURLConnection;

Import Java.net.URL;

Import android.app.Activity;

Import Android.os.Bundle;

Import Android.os.Handler;

Import Android.os.Message;

Import Android.text.TextUtils;

Import Android.view.View;

Import Android.widget.EditText;

Import Android.widget.Toast;

Import Com.itheima.htmlview.utils.StreamTools;

public class Mainactivity extends Activity {

private static final int ERROR = 0;

private static final int FAILED = 1;

private static final int SUCCESS = 2;

Private EditText et_qq;

Private EditText et_pwd;


// 1. Create a handler member variable in the main thread :

Private Handler Handler = new Handler () {

public void Handlemessage (Message msg) {

int what = Msg.what;

Switch (what) {

Case 0:

Toast.maketext (Mainactivity.this, " failed to access the network ", 0). Show ();

Break

Case 1:

Toast.maketext (mainactivity.this, " service returned data failed ", 0). Show ();

Break

Case 2:

String result = (string) msg.obj;

Toast.maketext (mainactivity.this, result, 0). Show ();

Break

}



};

};

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

ET_QQ = (EditText) Findviewbyid (R.ID.ET_QQ);

Et_pwd = (EditText) Findviewbyid (R.ID.ET_PWD);

}

public void Login (View v) {

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 (This, "QQ number or password cannot be empty ", 0). Show ();

Return

} else {

New Thread () {

public void Run () {

try {

1, create the URL object, open the HTTP type of connection;

String Path = "Http://192.168.0.10:8080/web/servlet/LoginServlet?username=" +qq+ "&password=" +PWD;

URL url = new URL (path);

HttpURLConnection conn = (httpurlconnection) URL

. OpenConnection ();

2, set the request header information:GET;

Conn.setrequestmethod ("GET");

to set the time-out for a connection

Conn.setconnecttimeout (3000);

Conn.setrequestproperty ("Accept-language", "ZH-CN");

3.get the response data returned by the server side, the input stream of the binary;

int code = Conn.getresponsecode ();

if (code = = 200) {

InputStream is = Conn.getinputstream ();

String result = Streamtools.readstream (IS);


Message msg = Message.obtain ();

message code to distinguish where a message is sent

Msg.what = SUCCESS;

Msg.obj = result;

Handler.sendmessage (msg);

}else{

Service returned data failed

Message msg = Message.obtain ();

// msg.obj = " service returned data failed ";

Msg.what = FAILED;

Handler.sendmessage (msg);

}

} catch (Exception e) {

E.printstacktrace ();


failed to access the network

Message msg = Message.obtain ();

// msg.obj = " failed to access the network ";

Msg.what = ERROR;

Handler.sendmessage (msg);

}

};

}.start ();

}

}

}


This article is from the "want to sell cut cake" blog, please be sure to keep this source http://dkpzjl.blog.51cto.com/9545260/1569869

Use get to submit data to server side under Android

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.