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
- Package com.infy.auction.client.util;
- Import java.io.IOException;
- Import java.util.ArrayList;
- Import java.util.List;
- Import Java.util.Map;
- Import Org.apache.http.HttpResponse;
- Import Org.apache.http.NameValuePair;
- Import org.apache.http.client.ClientProtocolException;
- Import org.apache.http.client.HttpClient;
- Import org.apache.http.client.entity.UrlEncodedFormEntity;
- Import Org.apache.http.client.methods.HttpGet;
- Import Org.apache.http.client.methods.HttpPost;
- Import org.apache.http.impl.client.DefaultHttpClient;
- Import Org.apache.http.message.BasicNameValuePair;
- Import Org.apache.http.util.EntityUtils;
- Import Android.util.Log;
- Public class Httputil {
- private static final String tag="Httputil";
- //Create Httpcilent Object
- public static HttpClient HttpClient = new Defaulthttpclient ();
- public static final String Base_url = "http://losthost:8080/auction/";
- //Send URL request, server response string
- public static string getrequest (string url) throws exception{
- //Create a HttpGet object
- HttpGet get = new HttpGet (URL);
- //Send GET request
- HttpResponse HttpResponse = Httpclient.execute (get);
- LOG.I (TAG, "Getreq ==getstatuscode--->" +httpresponse.getstatusline (). Getstatuscode ());
- //If the server succeeds in returning a response
- if (Httpresponse.getstatusline (). Getstatuscode () = = () {
- //Get the response string
- String result = entityutils.tostring (httpresponse.getentity ());
- LOG.I (TAG, "Getreq ==result--->" +result);
- return result;
- }
- return "";
- }
- //Send POST request
- public static string Postrequest (String url,map<string, string> rawparams) throws exception{
- LOG.I (TAG, "Postrequest--->begin");
- //Create HttpPost Object
- HttpPost post = new HttpPost (URL);
- //If the number of parameters passed is more, the passed parameters can be encapsulated
- list<namevaluepair> params = new arraylist<namevaluepair> ();
- For (String Key:rawParams.keySet ()) {
- //Package Request Parameters
- Params.add (new Basicnamevaluepair (Key, Rawparams.get (key)));
- }
- //Set parameters for the request
- Post.setentity (new urlencodedformentity (params,"Utf-8"));
- //Send POST request
- HttpResponse HttpResponse = Httpclient.execute (POST);
- //If the server succeeds in returning a response
- LOG.I (TAG, "httpl---->" +httpresponse.getstatusline (). Getstatuscode ());
- if (Httpresponse.getstatusline (). Getstatuscode () = = () {
- //Get the response string
- String result = entityutils.tostring (httpresponse.getentity ());
- LOG.I (TAG, "response-->" +result);
- return result;
- }
- return null;
- }
- }
Tool classes that display various dialog boxes: Dialog.xml
[Java]View Plaincopy
- Package com.infy.auction.client.util;
- Import android.app.Activity;
- Import Android.app.AlertDialog;
- Import Android.content.Context;
- Import Android.content.DialogInterface;
- Import Android.content.DialogInterface.OnClickListener;
- Import Android.view.View;
- Public class Dialogutil {
- //Define a dialog box to display the message
- public static void ShowDialog (final Context ctx, String msg, boolean closeself) {
- //TODO auto-generated method stub
- //Create a Alertdialog.builder object
- Alertdialog.builder Builder = new Alertdialog.builder (CTX). Setmessage (msg). Setcancelable (false);
- if (closeself) {
- Builder.setpositivebutton ("OK", new Onclicklistener () {
- @Override
- public void OnClick (Dialoginterface dialog, int which) {
- //TODO auto-generated method stub
- //End Current Activity
- (Activity) ctx). Finish ();
- }
- });
- }else{
- Builder.setpositivebutton ("OK", null);
- }
- Builder.create (). Show ();
- }
- //Define a dialog box that displays the specified component
- public static void ShowDialog (Context ctx,view View) {
- Alertdialog.builder Builder = new Alertdialog.builder (CTX). Setview (view). setcancelable (false). Setpositivebutton ("OK", null);
- Builder.create (). Show ();
- }
- }
Android Combat Chapter (III)