/**
* Created by RongGuang on 2015/9/19.
*/
public class RongHttp {
/**
* Http Post请求
* @param url
* @param pairs 参数
* @param clzz 对象
* @return Object 返回相应对象
* @throws Exception
*/
public Object post(String url, List<NameValuePair> pairs,Class clzz) throws Exception {
String json;
//可通过配置文件获取
String host="http://fanyi.youdao.com/";
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout",3000);
client.getParams().setParameter("http.connection.timeout",3000);
client.getParams().setParameter("http.connection-manager.timeout",60*60L);
HttpPost httpPost = new HttpPost(host + url);
httpPost.setHeader("User-Agent", "RongGuang Of Android");
httpPost.setHeader("userId", "RongGuang");
try {
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "utf-8"));
try {
HttpResponse httpResponse = client.execute(httpPost);
json = EntityUtils.toString(httpResponse.getEntity());
System.out.println(">>$"+httpPost.getURI());
} catch (IOException e) {
throw new Exception(e);
}
} catch (UnsupportedEncodingException e) {
throw new Exception(e);
}
return JSON.parseObject(json,clzz);
} /**
* Http get请求
* @param url
* @param pairs
* @param clzz
* @return
* @throws Exception
*/
public Object get(String url, List<NameValuePair> pairs,Class clzz) throws Exception {
String json;
// AppOption appOption = new AppOption();
String host="http://fanyi.youdao.com/";
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout",3000);
client.getParams().setParameter("http.connection.timeout",3000);
client.getParams().setParameter("http.connection-manager.timeout", 60 * 60L);
String param= URLEncodedUtils.format(pairs, "UTF-8");
HttpGet httpGet = new HttpGet(host + url+"?"+param);
httpGet.setHeader("User-Agent", "RongGuang Of Android");
httpGet.setHeader("userId", "RongGuang"); try {
// Log.i("d======", host + url + "?" + param);
HttpResponse httpResponse = client.execute(httpGet);
json = EntityUtils.toString(httpResponse.getEntity());
// System.out.println(">>$=================================="+json);
} catch (IOException e) {
throw new Exception(e);
}
return JSON.parseObject(json, clzz);
}
/**
* 异步http请求下载图片返回Drawable对象
*/
public Drawable post2Drawable(String url){
HttpPost httpPost=null;
HttpClient httpClient=null;
HttpResponse httpResponse=null;
try{
httpPost=new HttpPost(url);
httpClient=new DefaultHttpClient();
httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
InputStream is=httpResponse.getEntity().getContent();
return FormatTools.getInstance().InputStream2Drawable(is);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 异步http请求下载图片返回Bitmap对象
*/
public Bitmap post2Bitmap(String url){
HttpPost httpPost=null;
HttpClient httpClient=null;
HttpResponse httpResponse=null;
try{
httpPost=new HttpPost(url);
httpClient=new DefaultHttpClient();
httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
InputStream is=httpResponse.getEntity().getContent();
return FormatTools.getInstance().InputStream2Bitmap(is);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}

直接相应的位置调用即可。

例:(谨记:主线程中不能访问网络、子线程中不能更新UI)

   Bitmap bitmap=(SuperAsyncHttp.getInstance().post4Bitmap("http://img1.gtimg.com/news/pics/hv1/39/111/1927/125331519.jpg"));
iv.setImageBitmap(bitmap);
04-28 00:47