欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android调用webservice接口获取信息

程序员文章站 2023-11-27 12:54:52
我的有一篇博客上讲了如何基于cxf搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数。 1.在li...

我的有一篇博客上讲了如何基于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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。