开发工具:Andorid Studio 1.3
运行环境:Android 4.4 KitKat
写一个工具类来给主界面使用,作用是使用Ksoap访问特定的网站服务,获取返回的验证码图片字节码,发送Message给主界面
public class DownLoad implements Runnable {
private final String NAMESPACE = "http://WebXml.com.cn/";
private final String METHODNAME = "enValidateByte";
private final String SOAPACTION = "http://WebXml.com.cn/enValidateByte";
private final String URL = "http://webservice.webxml.com.cn/WebServices/ValidateCodeWebService.asmx";
@Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
Log.d("myDebug_str", MainActivity.str);
request.addProperty("byString", MainActivity.str);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transportSE = new HttpTransportSE(URL);
try {
transportSE.call(SOAPACTION, envelope);
} catch (Exception e) {
Log.d("myDebug_call", "failed to call SOAPACTION");
e.printStackTrace();
}
Log.d("myDebug_fault", envelope.bodyIn.toString());
SoapObject result = (SoapObject) envelope.bodyIn;
SoapPrimitive detail = (SoapPrimitive) result.getProperty("enValidateByteResult");
Message msg = new Message();
msg.what = MainActivity.GET_CODE;
msg.obj = detail.toString();
MainActivity.handler.sendMessage(msg);
Log.d("myDebug_sendMsg", "Send Msg successfully..");
} catch (Exception e) {
Message msg = new Message();
msg.what = MainActivity.ERROR_CODE;
MainActivity.handler.sendMessage(msg);
e.printStackTrace();
Log.d("myDebug", "Fail to finish the Progressing..");
}
}
}
这里需要注意的是,Http请求的时候没有设定超时时间,是根据默认值来设定的。整个流程中用了try语句捕捉异常,主要异常在于超时返回内容为空,可以手动捕捉该异常返回Message为异常给主界面。
主界面中点击Create的Button的时候,打开显示图片的boolean值的成员变量,说明用户等待接收验证码。点击Button后提高用户体验为在程序中控制关闭输入法的弹出。如果用户输入空值,提示用户输入内容。用ProgressDialog显示等待,该等待可取消,取消后返回的验证码也不显示
Button btnCreate = (Button)this.findViewById(R.id.btnCreate);
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
if (!edtStr.getText().toString().isEmpty()) {
toShowPic = true;
MainActivity.str = edtStr.getText().toString();
progressDialog = ProgressDialog.show(MainActivity.this, "Requesting", "Requesting...", true, true);
progressDialog.setOnCancelListener(progressDialogCancelListener);
workThread = new Thread(new DownLoad());
workThread.start();
} else {
imageView.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "请输入验证码字符!", Toast.LENGTH_SHORT).show();
}
}
});
public static DialogInterface.OnCancelListener progressDialogCancelListener = new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
toShowPic = false;
}
};
接着就是用Handler捕获消息,作出相应的处理。因为返回的内容是字节码,需要解码生成一个Bitmap图像。由于图像的大小不一,通过作变换矩阵Matrix拉伸或缩小到设备宽度,填充到ImageView中,使得用户体验提升
public static final int GET_CODE = 0;
public static final int ERROR_CODE = 1;
public static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case GET_CODE:
if (toShowPic) {
progressDialog.cancel();
byte[] data = Base64.decode((msg.obj.toString()).getBytes(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
float scale = (float) linearLayout.getWidth() / (float) bitmap.getWidth();
Log.d("myDebug", "scale = " + scale);
matrix.postScale(scale, scale);
Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(resizeBitmap);
imageView.setVisibility(View.VISIBLE);
}
break;
case ERROR_CODE:
if (toShowPic) {
progressDialog.cancel();
Toast.makeText(mContext, "获取验证码超时", Toast.LENGTH_SHORT).show();
}
break;
default:
super.handleMessage(msg);
break;
}
}
};
最后不忘记给app加上访问网络的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
初始化界面->点击Create界面->成功获取验证码界面->Create空白验证码界面->
中文测试->获取验证码超时界面
了解Android中网络通讯的多种方法,进行简单的总结
传送门:下载