日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android访问WebService(axis)

發布時間:2023/12/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android访问WebService(axis) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我自己發布的webService就是不能被android訪問,后面從網上查了下,我自己的webservice是用apache cxf發布的,不能訪問。后面改成axis發布后就能訪問了。具體原因不明確,望高手指點一二,現把axis發布的demo記錄如下。

前提:axis已經下載下來并部署到tomcat中。具體操作google或baidu。

一,服務器端

package com.zsxh.test;

?

/**

?* 這是個普通的java類,通過Axis將其發布成WebService

?* @author LZX

?* @time 2011-9-7

?*/

public class HelloService {

public String[] sayHello(){

? ? ? ? return new String[]{"one","two","three"};

? ? }

? ??

? ? public String sayHelloToPerson(String name){

? ? ? ? ?return "Hello " + name;

? ? }

?

}

將這個類編譯后拷貝到..\webapps\axis\WEB-INF\classes\下。

二,server-config.wsdd文件的編寫,該文件的位置..\webapps\axis\WEB-INF\。如果沒有,可以新建一個文件并命名為server-config.wsdd即可

<?xml version="1.0" encoding="UTF-8"?>

<deployment xmlns="http://xml.apache.org/axis/wsdd/"

? ? xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

?

? ? <globalConfiguration>

? ? ? ? <parameter name="sendMultiRefs" value="true" />

? ? ? ? <parameter name="disablePrettyXML" value="true" />

? ? ? ? <parameter name="dotNetSoapEncFix" value="true" />

? ? ? ? <parameter name="enableNamespacePrefixOptimization" value="false" />

? ? ? ? <parameter name="sendXMLDeclaration" value="true" />

? ? ? ? <parameter name="sendXsiTypes" value="true" />

? ? ? ? <parameter name="attachments.implementation"

? ? ? ? ? ? value="org.apache.axis.attachments.AttachmentsImpl" />

? ? </globalConfiguration>

?

? ? <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper" />

? ? <!--這是主要的,我們發布的服務-->

? ? <service name="HelloService" provider="java:RPC">

? ? ? ? <parameter name="className" value="com.zsxh.test.HelloService" />

? ? ? ? <parameter name="scope" value="request" />

? ? ? ?<parameter name="allowedMethods" value="*" />

? ? ? ?<operation name="sayHello" qname="operNS:sayHello" xmlns:operNS="http://test.zsxh.com" returnQName="sayHelloResult" returnType="rtns:ListOfHello" xmlns:rtns="http://test.zsxh.com" />

? ? <typeMapping

? ? ? ?xmlns:ns="http://localhost:8080/axis/services/HelloService"

? ? ? ?qname="ns:ListOfHello"

? ? ? ?type="java:java.lang.String[]"

? ? ? ?serializer="org.apache.axis.encoding.ser.ArraySerializerFactory"

? ? ? ?deserializer="org.apache.axis.encoding.ser.ArrayDeserializerFactory"

? ? ? ?encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> ? ?

? ? ? ? <messageReceiver ?class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />

? ? </service>

?

? ? <transport name="http">

? ? ? ?<requestFlow>

? ? ? ? ? ? <handler type="URLMapper" />

? ? ? ?</requestFlow>

? ? </transport>

</deployment>

?

三,啟動tomcat,在地址欄輸入http://192.168.0.102:8080/axis/,沒有報錯,并出現Apache-AXIS表示成功“192.168.0.102”是我機器的ip地址。

四,客戶端的編寫

android訪問Webservice需要用到第三方的工具包,ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar,可以從網上下載

新建個android項目,將下面的代碼拷貝進去即可。

布局文件:

1,main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:orientation="vertical"

? ? android:layout_width="fill_parent"

? ? android:layout_height="fill_parent"

? ? >

<TextView ?

? ? android:layout_width="fill_parent"?

? ? android:layout_height="wrap_content"?

? ? android:text="@string/hello"

? ? />

? ??

<TextView android:id="@+id/message"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

?

<EditText android:id="@+id/traceCodeText"

? ? ? ? ? ? android:layout_width="fill_parent"?

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:hint="@string/traceCodeText"

/>

?

<Button android:id="@+id/search"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/search_tex"

/>

? ?

<TextView android:id="@+id/result_text" ? ?

?android:layout_width="wrap_content" ? ??

?android:layout_height="wrap_content" ? ??

?android:layout_gravity="center_horizontal|center_vertical" />

</LinearLayout>

2,strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

? ? <string name="hello">Hello World, MainActivity!</string>

? ? <string name="app_name">WebServiceTest1</string>

? ? <string name="search_tex">提交</string>

? ? <string name="traceCodeText">請輸入名稱</string>

</resources>

java代碼:

package cn.vaga.testwsclient;

?

import java.util.Vector;

?

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.AndroidHttpTransport;

import org.ksoap2.transport.HttpTransportSE;

?

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

?

public class MainActivity extends Activity {

private static final String NAMESPACE = "http://testws.vaga.cn"; ? ?

? ? private static String URL = "http://192.168.0.102:8080/axis/services/HelloService";

? ? private static final String METHOD_NAME = "sayHello";

// ? ?private static final String METHOD_NAME = "sayHelloToPerson";

? ? private static String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME;

? ??

? ? static TextView resultView;

? ? TextView tv;

EditText et;

Button button;

? ? /** Called when the activity is first created. */

? ? @Override

? ? public void onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.main);

? ? ? ??

? ? ? ? et = (EditText) this.findViewById(R.id.traceCodeText);

? ? ? ? tv = (TextView) findViewById(R.id.message);

? ? ? ? resultView = (TextView) findViewById(R.id.result_text);

? ? ? ? button = (Button) findViewById(R.id.search);

? ? ? ? button.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

//得到輸入的名稱

String traceCode = et.getText().toString().trim();

if(traceCode.equals("") || traceCode == null || traceCode.length() < 4){

// 給出錯誤提示

et.setError("您輸入的名稱有誤或太短了!");

et.requestFocus();

// 將顯示查詢結果的TextView清空

tv.setText("");

return;

}

resultView.setText(getHello(traceCode));

}

? ? ? ? });

? ? }

? ? ?

? ? public static String getHello(String name) {

? ? ? ? try {

? ? ? ? ? ? SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);

// ? ? ? ? ? ?rpc.addProperty("name", name);

?

// ? ? ? ? ? ?AndroidHttpTransport ht = new AndroidHttpTransport(URL);

? ? ? ? ? ? HttpTransportSE ht = new HttpTransportSE(URL);

? ? ? ? ? ? ht.debug = true;

?

? ? ? ? ? ? SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

? ? ? ? ? ? ? ? ? ? SoapEnvelope.VER11);

? ? ? ? ? ??

? ? ? ? ? ? envelope.bodyOut = rpc;

? ? ? ? ? ? envelope.dotNet = true;

? ? ? ? ? ? envelope.setOutputSoapObject(rpc);

?

? ? ? ? ? ? ht.call(SOAP_ACTION, envelope);

? ? ? ? ? ??

// ? ? ? ? ? ?debug(LOG_TAG, "DUMP>> " + ht.requestDump);

// ? ? ? ? ? ?debug(LOG_TAG, "DUMP<< " + ht.responseDump);

? ? ? ? ? ? String ss = "";

? ? ? ? ? ? SoapObject result = (SoapObject) envelope.bodyIn;

? ? ? ? ? ? //返回單個的情況

// ? ? ? ? ? ?ss = result.getProperty("sayHelloToPersonReturn").toString();

? ? ? ? ? ? //返回結果是個數組的情況

? ? ? ? ? ? Vector vrct = (Vector) result.getProperty("sayHelloResult");

? ? ? ? ? ? for(int i = 0; i < vrct.size();i++){

? ? ? ? ? ? ? ? System.out.println( vrct.get(i));

? ? ? ? ? ? ? ? ss += vrct.get(i)+",";

? ? ? ? ? ? ? ?}

? ? ? ? ? ? return ss;

? ? ? ? } catch (Exception e) {

? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? ? return null;

? ? }

}

運行即可得到如圖所示界面效果。

最初發表于:http://qhxn-328-liaozx.blog.163.com/blog/static/876067320118813835587

2011-09-08 14:03:10

總結

以上是生活随笔為你收集整理的android访问WebService(axis)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。