先给出我们用到的工具类
1.发送请求的工具类
本实例采用HttpClient与服务器通信,用到了一个工具类对Httpclient进行封装:定义了两个方法来发送请求
getRequest:发送GET请求
postRequest :发送POST请求
HttpUtil.java(注意IP地址换成自己的IP地址,这个BASE_URL = "http://losthost:8080/auction/,要不然,你就)
- 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";
- //创建HttpCilent对象
- public static HttpClient httpClient = new DefaultHttpClient();
- public static final String BASE_URL = "http://losthost:8080/auction/";
- //发送url的请求,服务器响应字符串
- public static String getRequest(String url) throws Exception{
- //创建一个HttpGet对象
- HttpGet get = new HttpGet(url);
- //发送GET请求
- HttpResponse httpResponse = httpClient.execute(get);
- Log.i(TAG, "getReq ==getStatusCode--->" +httpResponse.getStatusLine().getStatusCode());
- //如果服务器成功地返回响应
- if(httpResponse.getStatusLine().getStatusCode() == 200){
- //获取响应的字符串
- String result = EntityUtils.toString(httpResponse.getEntity());
- Log.i(TAG, "getReq ==result--->" +result);
- return result;
- }
- return "";
- }
- //发送Post请求
- public static String postRequest(String url,Map<String, String> rawParams) throws Exception{
- Log.i(TAG, "postRequest--->begin");
- //创建HttpPost对象
- HttpPost post = new HttpPost(url);
- //如果传递的参数个数比较多,可以对传递的参数进行封装
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- for(String key:rawParams.keySet()){
- //封装请求的参数
- params.add(new BasicNameValuePair(key, rawParams.get(key)));
- }
- //设置请求的参数
- post.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
- //发送Post请求
- HttpResponse httpResponse = httpClient.execute(post);
- //如果服务器成功地返回响应
- Log.i(TAG, "HttpL---->" +httpResponse.getStatusLine().getStatusCode());
- if(httpResponse.getStatusLine().getStatusCode() == 200){
- //获取响应的字符串
- String result = EntityUtils.toString(httpResponse.getEntity());
- Log.i(TAG, "response-->" +result);
- return result;
- }
- return null;
- }
- }
显示各种对话框的工具类:Dialog.xml
- 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 {
- //定义一个显示消息的对话框
- public static void showDialog(final Context ctx , String msg, boolean closeSelf) {
- // TODO Auto-generated method stub
- //创建一个AlertDialog.Builder对象
- AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(msg).setCancelable(false);
- if(closeSelf){
- builder.setPositiveButton("确定", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- //结束当前Activity
- ((Activity)ctx).finish();
- }
- });
- }else{
- builder.setPositiveButton("确定", null);
- }
- builder.create().show();
- }
- //定义一个显示指定组件的对话框
- public static void showDialog(Context ctx,View view){
- AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setView(view).setCancelable(false).setPositiveButton("确定", null);
- builder.create().show();
- }
- }