Android Combat Chapter (III)

Source: Internet
Author: User

First, give us the tool class we use.

1. Tool class for sending requests

This example uses httpclient to communicate with the server and uses a tool class to encapsulate the HttpClient: two methods are defined to send a request

Getrequest: Send GET request

Postrequest: Send POST request

Httputil.java (Note that the IP address is replaced by its own IP address, this base_url = "http://losthost:8080/auction/, otherwise, you will)

[Java]View Plaincopy
  1. Package com.infy.auction.client.util;
  2. Import java.io.IOException;
  3. Import java.util.ArrayList;
  4. Import java.util.List;
  5. Import Java.util.Map;
  6. Import Org.apache.http.HttpResponse;
  7. Import Org.apache.http.NameValuePair;
  8. Import org.apache.http.client.ClientProtocolException;
  9. Import org.apache.http.client.HttpClient;
  10. Import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. Import Org.apache.http.client.methods.HttpGet;
  12. Import Org.apache.http.client.methods.HttpPost;
  13. Import org.apache.http.impl.client.DefaultHttpClient;
  14. Import Org.apache.http.message.BasicNameValuePair;
  15. Import Org.apache.http.util.EntityUtils;
  16. Import Android.util.Log;
  17. Public class Httputil {
  18. private static final String tag="Httputil";
  19. //Create Httpcilent Object
  20. public static HttpClient HttpClient = new Defaulthttpclient ();
  21. public static final String Base_url = "http://losthost:8080/auction/";
  22. //Send URL request, server response string
  23. public static string getrequest (string url) throws exception{
  24. //Create a HttpGet object
  25. HttpGet get = new HttpGet (URL);
  26. //Send GET request
  27. HttpResponse HttpResponse = Httpclient.execute (get);
  28. LOG.I (TAG, "Getreq ==getstatuscode--->" +httpresponse.getstatusline (). Getstatuscode ());
  29. //If the server succeeds in returning a response
  30. if (Httpresponse.getstatusline (). Getstatuscode () = = () {
  31. //Get the response string
  32. String result = entityutils.tostring (httpresponse.getentity ());
  33. LOG.I (TAG, "Getreq ==result--->" +result);
  34. return result;
  35. }
  36. return "";
  37. }
  38. //Send POST request
  39. public static string Postrequest (String url,map<string, string> rawparams) throws exception{
  40. LOG.I (TAG, "Postrequest--->begin");
  41. //Create HttpPost Object
  42. HttpPost post = new HttpPost (URL);
  43. //If the number of parameters passed is more, the passed parameters can be encapsulated
  44. list<namevaluepair> params = new arraylist<namevaluepair> ();
  45. For (String Key:rawParams.keySet ()) {
  46. //Package Request Parameters
  47. Params.add (new Basicnamevaluepair (Key, Rawparams.get (key)));
  48. }
  49. //Set parameters for the request
  50. Post.setentity (new urlencodedformentity (params,"Utf-8"));
  51. //Send POST request
  52. HttpResponse HttpResponse = Httpclient.execute (POST);
  53. //If the server succeeds in returning a response
  54. LOG.I (TAG, "httpl---->" +httpresponse.getstatusline (). Getstatuscode ());
  55. if (Httpresponse.getstatusline (). Getstatuscode () = = () {
  56. //Get the response string
  57. String result = entityutils.tostring (httpresponse.getentity ());
  58. LOG.I (TAG, "response-->" +result);
  59. return result;
  60. }
  61. return null;
  62. }
  63. }

Tool classes that display various dialog boxes: Dialog.xml

[Java]View Plaincopy
  1. Package com.infy.auction.client.util;
  2. Import android.app.Activity;
  3. Import Android.app.AlertDialog;
  4. Import Android.content.Context;
  5. Import Android.content.DialogInterface;
  6. Import Android.content.DialogInterface.OnClickListener;
  7. Import Android.view.View;
  8. Public class Dialogutil {
  9. //Define a dialog box to display the message
  10. public static void ShowDialog (final Context ctx, String msg, boolean closeself) {
  11. //TODO auto-generated method stub
  12. //Create a Alertdialog.builder object
  13. Alertdialog.builder Builder = new Alertdialog.builder (CTX). Setmessage (msg). Setcancelable (false);
  14. if (closeself) {
  15. Builder.setpositivebutton ("OK", new Onclicklistener () {
  16. @Override
  17. public void OnClick (Dialoginterface dialog, int which) {
  18. //TODO auto-generated method stub
  19. //End Current Activity
  20. (Activity) ctx). Finish ();
  21. }
  22. });
  23. }else{
  24. Builder.setpositivebutton ("OK", null);
  25. }
  26. Builder.create (). Show ();
  27. }
  28. //Define a dialog box that displays the specified component
  29. public static void ShowDialog (Context ctx,view View) {
  30. Alertdialog.builder Builder = new Alertdialog.builder (CTX). Setview (view). setcancelable (false).  Setpositivebutton ("OK", null);
  31. Builder.create (). Show ();
  32. }
  33. }

Android Combat Chapter (III)

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.