AdnroidUtils-常用工具类(showDiaLog/HTTP)
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();
}
}
AdnroidUtils-常用工具类(showDiaLog/HTTP)的更多相关文章
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- IOS开发--常用工具类收集整理(Objective-C)(持续更新)
前言:整理和收集了IOS项目开发常用的工具类,最后也给出了源码下载链接. 这些可复用的工具,一定会给你实际项目开发工作锦上添花,会给你带来大大的工作效率. 重复造轮子的事情,除却自我多练习编码之外,就 ...
- Apache Commons 常用工具类整理
其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包 public class ApacheCommonsT ...
- Android 常用工具类之SPUtil,可以修改默认sp文件的路径
参考: 1. 利用Java反射机制改变SharedPreferences存储路径 Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...
- javaweb常用工具类及配置文件备份
Javaweb常用工具类及配置文件备份 做一个代码备份,以后常用到的. hibernate工具类备份 package com.dly.service; /* * hibernate获取sessi ...
- [C#] 常用工具类——直接在浏览器输出数据
/// <summary> /// <para> </para> /// 常用工具类——直接在浏览器输出数据 /// <para> ---------- ...
- [C#] 常用工具类——加密解密类
using System; using System.Configuration; using System.Collections.Generic; using System.Text; using ...
- C#常用工具类——Excel操作类
/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...
- [C#] 常用工具类——系统日志类
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ...
随机推荐
- 集合—ArrayList
ArrayList也叫作数组列表 public static void main(String[] args) { List list1 = new ArrayList<String>() ...
- 【OpenStack 虚拟机初始化user-data & Cloud-init】
示例: import httplib import json import base64 tenant_id='xxx' token='xxx' compute_host="xxx" ...
- (转)spring boot实战(第三篇)事件监听源码分析
原文:http://blog.csdn.net/liaokailin/article/details/48194777 监听源码分析 首先是我们自定义的main方法: package com.lkl. ...
- No goals have been specified for this build 解决方案
运行maven报错:[ERROR] No goals have been specified for this build. You must specify a valid lifecycle ph ...
- jQuery UI加入效果
1.设计源代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- Linux学习笔记 (四)归档和压缩
一.zip压缩命令: 1.压缩文件: 格式:zip 压缩文件 源文件 例:zip abc.zip abc //将abc文件压缩到abc.zip文件内. 2.压缩目录: 格式:zip –r 压缩目录 ...
- mui 事件绑定(on)
除了可以使用addEventListener()方法监听某个特定元素上的事件外, 也可以使用.on()方法实现批量元素的事件绑定. 示例: 点击新闻列表,获取当前列表项的id,并将该id传给新闻详情页 ...
- js bind 绑定this指向
1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...
- java 线程的几种状态(转载)
java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在 ...
- a标签上的点击事件
当我们在处理a标签上的点击事件时发现即使href=""里面为空,点击事件的效果也不明显,这种情况该如何处理呢?常见的处理方法有以下几种: 1.a href="javasc ...