1. HttpUtils  该工具类应用于Android客户端+Web服务器

/**
*
*/
package com.nubb.auction.client.util; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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;
/**
* Description:利用Url请求Json格式的数据
*
* @author maoyun0903@163.com
* @version 1.0
*/
public class HttpUtil
{
// 创建HttpClient对象
public static HttpClient httpClient = new DefaultHttpClient();
public static final String BASE_URL =
"http://192.168.1.11:8080/auction/android/";
/**
*
* @param url 发送请求的URL
* @return 服务器响应字符串
* @throws Exception
*/
public static String getRequest(final String url)
throws Exception
{
FutureTask<String> task = new FutureTask<String>(
new Callable<String>()
{
@Override
public String call() throws Exception
{
// 创建HttpGet对象。
HttpGet get = new HttpGet(url);
// 发送GET请求
HttpResponse httpResponse = httpClient.execute(get);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine()
.getStatusCode() == 200)
{
// 获取服务器响应字符串
String result = EntityUtils
.toString(httpResponse.getEntity());
return result;
}
return null;
}
});
new Thread(task).start();
return task.get();
} /**
* @param url 发送请求的URL
* @param params 请求参数
* @return 服务器响应字符串
* @throws Exception
*/
public static String postRequest(final String url
, final Map<String ,String> rawParams)throws Exception
{
FutureTask<String> task = new FutureTask<String>(
new Callable<String>()
{
@Override
public String call() throws Exception
{
// 创建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, "gbk"));
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine()
.getStatusCode() == 200)
{
// 获取服务器响应字符串
String result = EntityUtils
.toString(httpResponse.getEntity());
return result;
}
return null;
}
});
new Thread(task).start();
return task.get();
}
}

2. DiaLogUtils    显示指定组件的对话框

/**
*
*/
package com.nubb.auction.client.util; import com.nubb.auction.client.AuctionClientActivity; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.View; /**
* Description:显示指定组件的对话框,并跳转至指定的Activity
* @author maoyun0903@163.com
* @version 1.0
*/
public class DialogUtil
{
// 定义一个显示消息的对话框
public static void showDialog(final Context ctx
, String msg , boolean goHome)
{
// 创建一个AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(ctx)
.setMessage(msg).setCancelable(false);
if(goHome)
{
builder.setPositiveButton("确定", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent i = new Intent(ctx , AuctionClientActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity(i);
}
});
}
else
{
builder.setPositiveButton("确定", null);
}
builder.create().show();
}
// 定义一个显示指定组件的对话框
public static void showDialog(Context ctx , View view)
{
new AlertDialog.Builder(ctx)
.setView(view).setCancelable(false)
.setPositiveButton("确定", null)
.create()
.show();
}
}

最新文章

  1. WPF控件
  2. 强制关闭tomcat
  3. 常用颜色的RGB值
  4. (一)学习JavaScript之setTimeout方法
  5. 利用SpringMVC参数绑定实现动态插入数据
  6. ViewDragHelper的使用
  7. [转] iOS应用架构谈 网络层设计方案
  8. 【笔记】《通俗详细地讲解什么是P和NP问题》的概念记录
  9. Linux上安装Redis
  10. java 动态代理 , 多看看。 多用用。
  11. Websql,应用程序缓存,WebWorkers,SSE,WebSocket
  12. 复活广州.net俱乐部
  13. ARM与FPGA通过spi通信设计1.spi基础知识
  14. CMDB服务器管理系统【s5day92】:服务器管理回顾
  15. PCL数据类型和ROS数据类型的转换
  16. Python12/10--前端之display/overflow使用/清浮动的方式
  17. 安装ClamAV对centos系统进行病毒查杀
  18. 【托业】【新托业TOEIC新题型真题】学习笔记7-题库二-&gt;P1~4
  19. springmvc 整合shiro
  20. [hadoop]mapreduce原理简述

热门文章

  1. &#39;telnet&#39; 不是内部或外部命令,也不是可运行的程序 或批处理文件。
  2. angularJS 中的two-way data binding.
  3. Quartz任务监听器
  4. Mybatis学习记录(一)---- 简单的CRUD
  5. 倍福TwinCAT(贝福Beckhoff)应用教程13.3 TwinCAT控制松下伺服 NC配合完整上位
  6. oneapm的技术博客(简书),用来追溯群里的讨论,mark
  7. HDU 5379 Mahjong tree(dfs)
  8. app接口开发(php)
  9. 基于Vue开发的tab切换组件
  10. POJ 3071 Football 【概率DP】