android http获取数据常用的方式:
1、Apache接口(HttpClient)
2、标准Java接口(HttpURLConnection)
3、Ksoap接口
其中HttpClient、 HttpURLConnection、都有对应的get,post方式。ksoap是用自己对应的一套接口主要针对webservice
2、遇到的获取图片的方式有几种:直接从url链接地址获取图片,从webservice上获取图片的字符数组。
很典型的以下链接就有这两种方式
http://webservice.webxml.com.cn/WebServices/ValidateCodeWebService.asmx
2.1、直接获取图片
2.1.1 HttpClient get
public void getHttpclientImage(String imageUrl) {
// httpGet连接对象
HttpGet httpRequest = new HttpGet(imageUrl);
// 取得HttpClient 对象
HttpClient httpclient = new DefaultHttpClient();
try {
// 请求httpClient ,取得HttpRestponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得相关信息 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
// 获得一个输入流
InputStream is = httpEntity.getContent();
//图片转换为bitmap格式
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//发给主线程
Message msg = Message.obtain();
msg.obj = bitmap;
handler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Message msg = Message.obtain();
msg.obj = e;
handler.sendMessage(msg);
}
}
2.1.2 httpURLConnection get
public void gethttpURLConnection(String imageUrl) {
// httpURLConnection方式解析
try {
URL url = new URL(imageUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
//获取输入流 解为bmp
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//发给主线程
Message msg = Message.obtain();
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (Exception e) {
}
}
note: 因为项目需要保存为本地图片,所以直接在发给主线程的时候转换为bitmap的格式,并且强调下数据量大的时候一般也是以流的形式处理获取数据
2.2 获取图片字符数组。参看http://webservice.webxml.com.cn/WebServices/ValidateCodeWebService.asmx 中的mallValidateByte 方法
头部有:<base64Binary xmlns="http://WebXml.com.cn/">
iVBORw0KGgoAAAANSUhEUg -------
尾部:</base64Binary>
所以还不能直接取出这些数据来生成图片,我就这么傻的认为获取的内容就是图片流,后来查了蛮多材料,才反应过来,这个头尾的意思是
public void getChartImage() {
// 命名空间
String nameSpace = "http://WebXml.com.cn/";
// 调用的方法名称
String methodName = "smallValidateByte";
// EndPoint
String endPoint = "http://webservice.webxml.com.cn/WebServices/ValidateCodeWebService.asmx";
// SOAP Action
String soapAction = nameSpace + methodName;
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
rpc.addProperty("byString", 5);
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 调用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.obj = e.toString();
handler.sendMessage(msg);
return;
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
//取出字符串部分
Object mObject = object.getProperty(0);
//Base64字符串还原
byte[] b = Base64.decode(result.getBytes(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
//bitmap对象返回主线程
Message msg = Message.obtain();
msg.obj = bitmap;
handler.sendMessage(msg);
}
note:
android自带64解包 import android.util.Base64;
3、关于
imageIView的几种界面填充方式参见: scaleType
4、存储bmp于本地png
try{
Bitmap mBitmap = (Bitmap) msg.obj;
File f = new File(dir.getPath() + '/' + mImageName + ".png");
f.createNewFile();
FileOutputStream fOut = null;
fOut = new FileOutputStream(f);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
mBitmap = null;
} catch (Exception e) {
// TODO: handle exception
}