博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android调用webservice接口获取信息
阅读量:5282 次
发布时间:2019-06-14

本文共 7420 字,大约阅读时间需要 24 分钟。

我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数。

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

 

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

 

3.接下来就要通过代码调用借口了

String WSDL_URI = "http://localhost:8080/WebService4/getNewIdPort?wsdl";//wsdl 的uriString namespace = "http://services.he.TSD/";//namespaceString 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;//由于是发送请求,所以是设置bodyOutenvelope.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
> mData;// 存储的EditText值 private ArrayList mData=new ArrayList(); //private ArrayList mDate = new ArrayList(); public Map
editorValue = new HashMap
();// 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; }}
View Code

 

8.调如何用ListAdapter类

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

 

转载于:https://www.cnblogs.com/waterlily/p/7819674.html

你可能感兴趣的文章
[CF#250 Div.2 D]The Child and Zoo(并查集)
查看>>
博客园博客插入公式
查看>>
hdu 1028 Ignatius and the Princess III(母函数入门+模板)
查看>>
Ubuntu下配置安装telnet server
查看>>
Codeforces 235 E Number Challenge
查看>>
ubuntu 常见命令整理
查看>>
EJBCA安装教程+postgresql+wildfly10
查看>>
(五十四)涂鸦的实现和截图的保存
查看>>
配置EditPlus使其可以编译运行java程序
查看>>
java中的占位符\t\n\r\f
查看>>
MySQL通过frm 和 ibd 恢复数据过程
查看>>
SRS源码——Listener
查看>>
Java面向对象抽象类案例分析
查看>>
对SPI、IIC、IIS、UART、CAN、SDIO、GPIO的解释
查看>>
Thymeleaf模板格式化LocalDatetime时间格式
查看>>
庖丁解“学生信息管理系统”
查看>>
Pyltp使用
查看>>
其他ip无法访问Yii的gii,配置ip就可以
查看>>
php做的一个简易爬虫
查看>>
x的x次幂的值为10,求x的近似值
查看>>