Android之连接WebServices操作SqlSever数据库返回数据

#1、写一个帮助类DBUtil

方法callWS,访问Wb返回相对应的数据
/**
String namespace = “http://tempuri.org/”;//namespace
String Url = “http://47.92.68.57:8099/WebServices_Device_Management.asmx?WSDL”;
String methodName = “Select”;
有参数的时候:
Map<String, Object> params = new HashMap<>();
params.put(“username”, “admin”);
params.put(“password”, “123456”);

  • @param nameSpace WS的命名空间

  • @param methodName WS的方法名

  • @param wsdl WS的wsdl的完整路径名

  • @param params WS的方法所需要的参数

  • @return SoapObject对象
    */
    public static SoapObject callWS(String nameSpace, String methodName,
    String wsdl, Map<String, Object> params) {
    final String SOAP_ACTION = nameSpace + methodName;
    SoapObject soapObject = new SoapObject(nameSpace, methodName);

    if ((params != null) && (!params.isEmpty())) {
    Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry<String, Object> e = (Map.Entry<String, Object>) ((Iterator) it).next();
    soapObject.addProperty(e.getKey(), e.getValue());
    }
    }

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
    envelope.bodyOut = soapObject;
    // 兼容.NET开发的Web Service
    envelope.dotNet = true;
    envelope.encodingStyle=“UTF-8”;

    HttpTransportSE ht = new HttpTransportSE(wsdl);
    try {
    ht.getServiceConnection();
    ht.call(SOAP_ACTION, envelope);
    if (envelope.getResponse() != null) {
    SoapObject result = (SoapObject) envelope.bodyIn;
    return result;
    } else {
    return null;
    }
    } catch (Exception e) {
    Log.e(“error”, e.getMessage());
    }
    return null;
    }

方法二convertJSON2List 本案例Wb返回数据是Json格式
/**

  • @param result JSON字符串

  • @param name JSON数组名称

  • @param fields JSON字符串所包含的字段

  • @return 返回List<Map<String , Object>>类型的列表,Map<String,Object>对应于 “id”:"1"的结构
    /
    public static List<Map<String, Object>> convertJSON2List(String result,String name, String[] fields) {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    try {
    JSONArray array = new JSONObject(result).getJSONArray(name);
    for (int i = 0; i < array.length(); i++) {
    JSONObject object = (JSONObject) array.opt(i);
    Map<String, Object> map = new HashMap<String, Object>();
    for (String str : fields) {
    map.put(str, object.get(str));
    }
    list.add(map);
    }
    } catch (JSONException e) {
    Log.e(“error”, e.getMessage());
    }
    return list;
    }
    #2在一个Avtivity中写一个点击事件
    /
    *

    • 初始化数据
      */
      private void init() {
      loginAccount = (EditText) findViewById(R.id.login_account);
      loginPassword = (EditText) findViewById(R.id.login_password);
      loginBtn = (Button) findViewById(R.id.login_btn);
      registerBtn = (Button) findViewById(R.id.register_btn);
      //点击登录按钮
      loginBtn.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           DBUtil db = new DBUtil("1", "2");//调用数据库查询类
           String namespace = "http://tempuri.org/";//namespace
           //String soap_action = "http://tempuri.org/Check_User";
           String Url = "http://47.92.68.57:8099/WebServices_Device_Management.asmx?WSDL";
           String methodName = "Select";
      
           Map<String, Object> params = new HashMap<>();
           params.put("username", "admin");
           params.put("password", "123456");
      
      
           SoapObject soapObject = db.callWS(namespace, methodName,
                   Url, null);
      
           if (soapObject != null) {
      
               String detail = soapObject.getProperty("SelectResult").toString();
               //Toast.makeText(LoginActivity.this, detail, Toast.LENGTH_SHORT).show();
      
               try {
                   //将JSON字符串转换为List的结构
                   List<Map<String, Object>> list = DBUtil.convertJSON2List(
                           detail, "Result_List", new String[]{"id",
                                   "username", "password", "email"});
                   //通过Intent将List传入到新的Activity-----本案例GridViewTestActivity
                   Intent newIntent = new Intent(LoginActivity.this, GridViewTestActivity.class);
                   Bundle bundle = new Bundle();
                   //List一定是一个Serializable类型
                   bundle.putSerializable("key", (Serializable) list);
                   newIntent.putExtras(bundle);
                   //启动新的Activity
                   startActivity(newIntent);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           } else {
               System.out.println("This is null...");
           }
       }
      

      });
      #三、在新的GridViewTestActivity中
      public class GridViewTestActivity extends AppCompatActivity {
      GridView grid;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_grid_view_test);
      Intent intent=this.getIntent();
      Bundle bundle=intent.getExtras();
      //获得传进来的List<Map<String,Object>>对象
      @SuppressWarnings(“unchecked”)
      List<Map<String, Object>> list = (List<Map<String, Object>>) bundle
      .getSerializable(“key”);
      Map<String, Object> l=list.get(0);
      String s=l.get(“username”).toString();
      //通过findViewById方法找到GridView对象
      grid = (GridView) findViewById(R.id.grid01);
      //SimpleAdapter适配器填充
      //1.context 当前上下文,用this表示,或者GridViewTest.this
      //2.data A List of Maps.要求是List<Map<String,Object>>结构的列表,即数据源
      //3.resource 布局文件
      //4.from 从哪里来,即提取数据源List中的哪些key
      //5.to 到哪里去,即填充布局文件中的控件
      SimpleAdapter adapter = new SimpleAdapter(this, list,
      R.layout.list_item, new String[] { “id”, “username”,“password”,“email” },
      new int[] { R.id.id, R.id.name, R.id.password,R.id.email });

      //将GridView和适配器绑定
      grid.setAdapter(adapter);
      }
      }
      四、GridViewTestActivity对应的布局文件

    <GridView
    android:id="@+id/grid01"
    android:layout_width=“fill_parent”
    android:layout_height=“wrap_content”
    android:horizontalSpacing=“3pt”
    android:verticalSpacing=“1pt”
    android:numColumns=“4”

     android:gravity="center"/>
    
五、新建list_item布局文件
<TextView
    android:id="@+id/id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

六、最终显示界面
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

期待mizi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值