android 后台处理数据+进度条

本文介绍了一个实用的 Java 工具类用于处理 Map 数据,并演示了如何利用 AsyncTask 执行后台数据查询任务,同时展示了进度条对话框的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

map中可以获取逐条数据.

package com.google.zxing.client.android.util;

import java.util.HashMap;
import java.util.Map;

public class MapUtil {

// 接收服务端的map,从map中可以获取逐条数据.
public static Map<String, String> getMap(String a) {
Map<String, String> goods = new HashMap<String, String>();
String[] str = a.trim().split("¥n/¥");
for (int i = 0; i < str.length; i++) {
String[] str1 = str[i].trim().split("¥n");
if (str1.length > 1) {
goods.put(str1[0], str1[1]);
} else {

goods.put(str1[0], "");

}
}
return goods;
}


}


/**
* 使用异步类上传数据 这里面请求服务器并且在数据传递结束后关闭了进度条
*/
private class DoPOst extends AsyncTask<Void, Integer, Void> {

String s;

// 接收服务端已Map形式传来的字符串
private Map<String, String> goods;

@Override
protected Void doInBackground(Void... arg0) {

try {
s = queryBFMW(resultNum);
Log.i("Other", "服务端查询后结果-------"+s+"------");
goods = MapUtil.getMap(s);

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
if (s == null || "".equals(s) || "网络异常".equals(s)) {
Toast.makeText(getApplicationContext(), "抱歉,未查到数据", 1)
.show();

} else {
/**
* 接收服务端字符串,通过Key 接收Value.
*/
String checkNum = goods.get("checking");

String codeContent = goods.get("code");
String kymtContent = goods.get("kymt");


if ("0".equals(checkNum.trim())) {
Toast.makeText(Zs.this, "..........!", 1).show();
return;
} else if ("1".equals(checkNum.trim())) {

if("null".equals(kymtContent)||"".equals(kymtContent) || kymtContent == null ){
return;
}else
// if("hsm".equalsIgnoreCase(checkNum))
{
kymtNumber = Integer.parseInt(kymtContent.trim());
Log.i("Other", "-----"+kymtNumber+"----------");
}

if (codeContent == null || "".equals(codeContent)) {
Toast.makeText(getApplicationContext(), ".........",
1).show();

return;
}

if (goods != null) {

hsmEt.setText(codeContent);
}

}
else if ("2".equals(checkNum.trim())) {
Toast.makeText(getApplicationContext(), ".....", 1).show();
return;
}
else if ("3".equals(checkNum.trim())) {
Toast.makeText(getApplicationContext(), ".....!", 1).show();
return;
}




/**
* 处理其他问题
*/
else {

}
}

super.onPostExecute(result);
}
}
/**
*
* @param resultNum
* @return
*/
private String queryBFMW(String resultNum) {
String ss = resultNum;
String url = HttpUtil.BASE_URL
+ "/servlet/HsmCodeServlet?type=sjzsmw&hsmCode=" + resultNum
+ "&userID=" + userName + "&typeName=bf";
Log.i("Other", "------"+url+"-----------");
return HttpUtil.queryStringForPost(url);
}

以下为进度条实现

private AlertDialog dialog;

// 显示对话框
View view = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.progress_dialog, null);
dialog = new AlertDialog.Builder(this).setView(view).create();
dialog.show();

// 初始化
DoPost doPost = new DoPost();
doPost.execute();

progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ProgressBar
android:id="@+id/progress_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/bbs_progress" />

<TextView
android:id="@+id/processText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/progress_icon"
android:layout_alignTop="@+id/progress_icon"
android:layout_toRightOf="@+id/progress_icon"
android:gravity="center"
android:text="请稍后..." >
</TextView>

</RelativeLayout>

bbs_progress.xml

<?xml version="1.0" encoding="UTF-8"?>
<rotate
android:drawable="@drawable/bbs_waiting"
android:useLevel="true"
android:fromDegrees="0.0"
android:toDegrees="360.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
xmlns:android="http://schemas.android.com/apk/res/android" />

bbs_btn_check.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ProgressBar
android:id="@+id/progress_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/bbs_progress" />

<TextView
android:id="@+id/processText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/progress_icon"
android:layout_alignTop="@+id/progress_icon"
android:layout_toRightOf="@+id/progress_icon"
android:gravity="center"
android:text="请稍后..." >
</TextView>

</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值