中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android調用webservice接口獲取信息

發布時間:2020-09-08 13:12:26 來源:腳本之家 閱讀:160 作者:水波粼粼 欄目:移動開發

我的有一篇博客上講了如何基于CXF搭建webservice,service層的接口會被部署到tomcat上,這一篇我就講一下如何在安卓中調用這些接口傳遞參數。

1.在lib中放入ksoap2的jar包并導入

2.在xml 配置文件中加入:

<!-- 訪問網絡的權限 --> 
  <uses-permission android:name="android.permission.INTERNET" /> 

3.接下來就要通過代碼調用借口了

String WSDL_URI = "http://localhost:8080/WebService4/getNewIdPort?wsdl";//wsdl 的uri
String namespace = "http://services.he.TSD/";//namespace
String methodName = "getNew";//要調用的方法名稱
// 創建HttpTransportSE對象
HttpTransportSE ht = new HttpTransportSE(WSDL_URI);
ht.debug = true;
// 使用soap1.1協議創建Envelop對象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// 實例化SoapObject對象
SoapObject object = new SoapObject(namespace, methodName);
// 設置參數
object.addProperty("arg0", "hhh");
// 將SoapObject對象設置為SoapSerializationEnvelope對象的傳出SOAP消息
envelope.bodyOut = object;//由于是發送請求,所以是設置bodyOut
envelope.dotNet =false;
envelope.setOutputSoapObject(object);
try{
 // 調用webService
 ht.call(null, envelope);
 System.out.println("回傳的值 :"+envelope.getResponse());
}
catch (Exception e) {
 e.printStackTrace();
 String msg=e.getMessage();
 System.out.println(msg);
}

4.若webservice接收不到android傳來的值:

  (1)參數名不對

  (2).net的webservice要將envelope.dotNet =true,否則envelope.dotNet =false

5.由于是部署到tomcat上的,為了同處于一個網段,要手機和電腦連一個wifi。 ipv4查找用window+R,接著cmd,最后ipconfig 即可查到。 

6.要關閉windows防火墻

7.當時我還遇到了一個問題,就是當傳遞的信息復雜時,比如傳遞list型的一個自定義的類,會出現問題,當時我是重寫了ListAdapter類,如下所示:

package com.adapter;


/**
 * Created by hexiaolu on 2017/4/13.
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.example.administrator.stock2.R;


public class ListAdapter extends BaseAdapter {
  HashMap item = new HashMap();
  private LayoutInflater mInflater;
  //private List<Map<String, Object>> mData;// 存儲的EditText值
  private ArrayList mData=new ArrayList();
  //private ArrayList mDate = new ArrayList();
  public Map<String, String> editorValue = new HashMap<String, String>();//
  private TextView spendMoney;
  public ListAdapter(Context context, ArrayList data,TextView spend) {
    spendMoney=spend;
    mData = data;
    mInflater = LayoutInflater.from(context);
    init();
  }

  // 初始化
  private void init() {
    editorValue.clear();
  }

  @Override
  public int getCount() {
    return mData.size();
  }

  @Override
  public Object getItem(int position) {
    return null;
  }

  @Override
  public long getItemId(int position) {
    return 0;
  }

  private Integer index = -1;

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    // convertView為null的時候初始化convertView。
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.list_item, null);
      holder.goodsName = (TextView) convertView
          .findViewById(R.id.list_item_goodsName);
      holder.goodsSellPrice = (TextView) convertView
          .findViewById(R.id.list_item_goodsSellPrice);
      holder.goodsUnit = (TextView) convertView
          .findViewById(R.id.list_item_goodsUnit);
      holder.goodsSellNum = (EditText) convertView
          .findViewById(R.id.list_item_goodsSellNum);
      holder.goodsSellNum.setTag(position);
      /*holder.goodsSellNum.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
          if (event.getAction() == MotionEvent.ACTION_UP) {
            index = (Integer) v.getTag();
          }
          return false;
        }
      });*/
      class MyTextWatcher implements TextWatcher {
        public MyTextWatcher(ViewHolder holder) {
          mHolder = holder;
        }

        private ViewHolder mHolder;

        @Override
        public void onTextChanged(CharSequence s, int start,
                     int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                       int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
          if (s != null && !"".equals(s.toString())) {
            int position = (Integer) mHolder.goodsSellNum.getTag();

            /*mData.get(position).put("goodsSellNum",s.toString());// 當EditText數據發生改變的時候存到data變量中*/
            item=(HashMap)mData.get(position);
            item.put("goodsSellNum",s.toString());
            mData.set(position,item);
            spendMoney.setText(String.valueOf(calculate()));//計算總價格
          }
        }
      }
      holder.goodsSellNum.addTextChangedListener(new MyTextWatcher(holder));
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
      holder.goodsSellNum.setTag(position);
    }
    item=(HashMap)mData.get(position);
    Object value=item.get("goodsName");
    //Object value = mData.get(position).get("list_item_goodsName");
    if (value != null) {
      holder.goodsName.setText((String) value);
    }
    item=(HashMap)mData.get(position);
    value=item.get("goodsSellPrice");
    //value = mData.get(position).get("list_item_goodsSellPrice");
    if (value != null) {
      holder.goodsSellPrice.setText(value.toString());
    }
    item=(HashMap)mData.get(position);
    value=item.get("goodsUnit");
    //value = mData.get(position).get("list_item_goodsUnit");
    if (value != null) {
      holder.goodsUnit.setText(value.toString());
    }
    item=(HashMap)mData.get(position);
    value=item.get("goodsSellNum");
    //value = mData.get(position).get("list_item_goodsSellNum");
    if (value != null && !"".equals(value)) {
      holder.goodsSellNum.setText(value.toString());
    }
    holder.goodsSellNum.clearFocus();
    if (index != -1 && index == position) {
      holder.goodsSellNum.requestFocus();
    }
    return convertView;
  }

  public final class ViewHolder {
    public TextView goodsName;
    public TextView goodsSellPrice;
    public EditText goodsSellNum;// ListView中的輸入
    public TextView goodsUnit;// 用來定義的標志性主鍵,可不用關心
  }
  public float calculate() {
    float money = 0;
    for (int i = 0; i < mData.size(); i++) {
      HashMap item = new HashMap();
      item = (HashMap) mData.get(i);
      float num = Float.parseFloat(item.get("goodsSellNum").toString());
      float price = Float.parseFloat(item.get("goodsSellPrice").toString());
      money = money + num * price;
    }
    return money;
  }
}

8.調如何用ListAdapter類

ListAdapter adapter = new ListAdapter(this, list,spendMoney); 
lv_goodsList.setAdapter(adapter);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

库车县| 新龙县| 固安县| 曲阜市| 枣庄市| 乌海市| 石渠县| 茶陵县| 莲花县| 吕梁市| 东兰县| 揭阳市| 特克斯县| 休宁县| 塔城市| 阿拉善盟| 浦江县| 万州区| 边坝县| 泸水县| 健康| 互助| 龙口市| 紫金县| 利辛县| 建昌县| 牙克石市| 宁晋县| 嘉黎县| 南木林县| 攀枝花市| 衡东县| 阳高县| 手机| 兴宁市| 临江市| 尼木县| 葫芦岛市| 阜康市| 霍林郭勒市| 青海省|