码迷,mamicode.com
首页 > 移动开发 > 详细

Android之实战篇(三)

时间:2014-09-03 14:40:26      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   os   io   java   strong   ar   for   

先给出我们用到的工具类

1.发送请求的工具类

本实例采用HttpClient与服务器通信,用到了一个工具类对Httpclient进行封装:定义了两个方法来发送请求

getRequest:发送GET请求

postRequest :发送POST请求

HttpUtil.java(注意IP地址换成自己的IP地址,这个BASE_URL = "http://losthost:8080/auction/,要不然,你就bubuko.com,布布扣)

  1. package com.infy.auction.client.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.http.HttpResponse;  
  9. import org.apache.http.NameValuePair;  
  10. import org.apache.http.client.ClientProtocolException;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  13. import org.apache.http.client.methods.HttpGet;  
  14. import org.apache.http.client.methods.HttpPost;  
  15. import org.apache.http.impl.client.DefaultHttpClient;  
  16. import org.apache.http.message.BasicNameValuePair;  
  17. import org.apache.http.util.EntityUtils;  
  18.   
  19. import android.util.Log;  
  20.   
  21. public class HttpUtil {  
  22.   
  23.     private static final String TAG="HttpUtil";  
  24.     //创建HttpCilent对象  
  25.     public static HttpClient httpClient = new DefaultHttpClient();  
  26.     public static final String BASE_URL = "http://losthost:8080/auction/";  
  27.       
  28.     //发送url的请求,服务器响应字符串  
  29.     public static String getRequest(String url) throws  Exception{  
  30.         //创建一个HttpGet对象  
  31.         HttpGet get = new HttpGet(url);  
  32.         //发送GET请求  
  33.         HttpResponse httpResponse = httpClient.execute(get);  
  34.         Log.i(TAG, "getReq ==getStatusCode--->" +httpResponse.getStatusLine().getStatusCode());  
  35.         //如果服务器成功地返回响应  
  36.         if(httpResponse.getStatusLine().getStatusCode() == 200){  
  37.             //获取响应的字符串  
  38.             String result = EntityUtils.toString(httpResponse.getEntity());  
  39.             Log.i(TAG, "getReq ==result--->" +result);  
  40.             return result;  
  41.         }  
  42.         return "";  
  43.     }  
  44.     //发送Post请求  
  45.     public static String postRequest(String url,Map<String, String> rawParams) throws Exception{  
  46.       
  47.         Log.i(TAG, "postRequest--->begin");  
  48.           
  49.         //创建HttpPost对象  
  50.         HttpPost post = new HttpPost(url);  
  51.         //如果传递的参数个数比较多,可以对传递的参数进行封装  
  52.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  53.         for(String key:rawParams.keySet()){  
  54.             //封装请求的参数  
  55.             params.add(new BasicNameValuePair(key, rawParams.get(key)));  
  56.               
  57.         }  
  58.         //设置请求的参数  
  59.         post.setEntity(new UrlEncodedFormEntity(params,"utf-8"));  
  60.         //发送Post请求  
  61.         HttpResponse httpResponse = httpClient.execute(post);  
  62.         //如果服务器成功地返回响应  
  63.         Log.i(TAG, "HttpL---->" +httpResponse.getStatusLine().getStatusCode());  
  64.         if(httpResponse.getStatusLine().getStatusCode() == 200){  
  65.             //获取响应的字符串  
  66.             String result = EntityUtils.toString(httpResponse.getEntity());  
  67.             Log.i(TAG, "response-->" +result);  
  68.             return result;  
  69.         }  
  70.           
  71.     return null;      
  72.     }  
  73.       

 

 

 

 

显示各种对话框的工具类:Dialog.xml

  1. package com.infy.auction.client.util;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.content.DialogInterface;  
  7. import android.content.DialogInterface.OnClickListener;  
  8. import android.view.View;  
  9.   
  10. public class DialogUtil {  
  11.   
  12.     //定义一个显示消息的对话框  
  13.     public static void showDialog(final Context ctx , String msg, boolean closeSelf) {  
  14.         // TODO Auto-generated method stub  
  15.         //创建一个AlertDialog.Builder对象  
  16.         AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(msg).setCancelable(false);  
  17.           
  18.         if(closeSelf){  
  19.             builder.setPositiveButton("确定", new OnClickListener() {  
  20.                   
  21.                 @Override  
  22.                 public void onClick(DialogInterface dialog, int which) {  
  23.                     // TODO Auto-generated method stub  
  24.                 //结束当前Activity  
  25.                     ((Activity)ctx).finish();  
  26.                 }  
  27.             });  
  28.         }else{  
  29.             builder.setPositiveButton("确定", null);  
  30.         }  
  31.         builder.create().show();  
  32.     }  
  33.   
  34.     //定义一个显示指定组件的对话框  
  35.     public static void showDialog(Context ctx,View view){  
  36.         AlertDialog.Builder  builder = new AlertDialog.Builder(ctx).setView(view).setCancelable(false).setPositiveButton("确定", null);  
  37.         builder.create().show();  
  38.     }  

Android之实战篇(三)

标签:android   blog   http   os   io   java   strong   ar   for   

原文地址:http://www.cnblogs.com/qiuyang1/p/3953552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!