Android uses HttpClient to access the server's servlet

Source: Internet
Author: User

Enter the user name and password on the logon page. After Successful Logon, the system jumps to the homepage. If the logon fails, an error message is displayed. When passing parameters, use the List of BasicNameValuePair as httpPost. setEntity. You cannot use HttpParams. [Java] 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 ht TpClient = new DefaultHttpClient (); String address = strUrl; HttpPost httpPost = new HttpPost (address); List <BasicNameValuePair> params = new parameter List <BasicNameValuePair> (); params. add (new BasicNameValuePair ("username", userName. getText (). toString (); params. add (new BasicNameValuePair ("password", password. getText (). toString (); httpPost. setEntity (new UrlEncodedFormEntity (params, "UTF-8"); // set Enter HttpResponse httpResponse = httpClient.exe cute (httpPost) in the POST Entity parameter;/* receive */if (httpResponse. getStatusLine (). getStatusCode () = 200) {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 = j SonResult. 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 connection problem. CODE: "+ httpResponse. getStatusLine (). getStatusCode ();} catch (JSONException e) {e. printStackTrace ();} catch (UnsupportedEncodingException e) {e. printStackTrace ();} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}});

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.