Android Connection Server servlet

Source: Internet
Author: User
Tags gettext

Login page to enter the user name and password, login successful to jump to the main page, failure to display error messages. When passing the parameter, use the Basicnamevaluepair list to httppost.setentity the way, cannot use Httpparams.

UserName = (edittext) This.findviewbyid (r.id.edittextusername);
Password = (edittext) This.findviewbyid (R.id.edittextpassword);
ErrorMsg = (TextView) This.findviewbyid (r.id.textviewloginmsg);
Button loginbtn = (button) This.findviewbyid (R.id.btnlogin);
		Loginbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View arg0) {String result;
			/* Send part */try {httpclient httpclient = new Defaulthttpclient ();
			String address = strURL;
			HttpPost HttpPost = new HttpPost (address);
			list<basicnamevaluepair> params = new linkedlist<basicnamevaluepair> ();
			Params.add (New Basicnamevaluepair ("username", Username.gettext (). toString ()));
			Params.add (New Basicnamevaluepair ("Password", Password.gettext (). toString ())); Httppost.setentity (New urlencodedformentity (params, "utf-8"));
			Fill in post entity HttpResponse HttpResponse = Httpclient.execute (HttpPost); /* Receive/if (Httpresponse.getstatusline (). Getstatuscode () = () {resUlt = entityutils.tostring (Httpresponse.getentity ());
				
				* * JSON text parsing/Jsontokener Jsonparser = new Jsontokener (result);
				Jsonobject Jsonresult = (jsonobject) jsonparser.nextvalue ();
				String status = Jsonresult.getstring ("LoginStatus");
				String outmessage = jsonresult.getstring ("Outmessage");
					if (Status.equals ("true")) {Intent i = new Intent (loginactivity.this, Mainactivity.class);
					I.putextra ("username", outmessage);
				Startactivityforresult (i, First_request_code);
					else {errormsg.setvisibility (view.visible);
				Errormsg.settext (Outmessage);
				} else {errormsg.setvisibility (view.visible); Errormsg.settext ("Network connectivity issues.")
			CODE: "+ httpresponse.getstatusline (). Getstatuscode ());
		} catch (Jsonexception e) {e.printstacktrace ();
		catch (Unsupportedencodingexception e) {e.printstacktrace ();
		catch (Clientprotocolexception e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace (); }


	}
}); 
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.