先给出我们用到的工具类
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";
-
- public static HttpClient httpClient = new DefaultHttpClient();
- public static final String BASE_URL = "http://losthost:8080/auction/";
-
-
- public static String getRequest(String url) throws Exception{
-
- HttpGet get = new HttpGet(url);
-
- 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 "";
- }
-
- public static String postRequest(String url,Map<String, String> rawParams) throws Exception{
-
- Log.i(TAG, "postRequest--->begin");
-
-
- 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"));
-
- 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) {
-
-
- 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) {
-
-
- ((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();
- }
- }
Android之实战篇(三)
原文:http://www.cnblogs.com/qiuyang1/p/3953552.html