android 后台处理数据+进度条

来源:互联网 发布:梦里花落知多少书包网 编辑:程序博客网 时间:2024/05/18 00:15

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>

原创粉丝点击