当前位置: 代码迷 >> Web前端 >> Android下应用WebService
  详细解决方案

Android下应用WebService

热度:600   发布时间:2012-09-19 13:43:53.0
Android上应用WebService

?

在做项目时,应用了WCF,在传输数据时,首先把数据转换成字符串,这样,就会产生很多问题,例如:如果数据是出现一些特殊符号(?、#等)这样数据就会出错。现在我要扩展功能,发现WCF的最大弊端,WCF是不能传数据流的,也我是我的能力有限。最后还是决定把WCF换回WebService,在这里给大家一个小例子,供大家参考,有什么不懂的发邮件给我:

用WebService需要一个jar包,可以去官网上下:http://code.google.com/p/k/ksoap2-android/downloads/list

Main.java

package com.action;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
 * @Author: hilary
 * @Date: 2011-2-12
 **/
public class Main extends Activity implements OnClickListener {

	private EditText mEditText;
	private TextView mTextView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
		mEditText = (EditText) findViewById(R.id.edittext);
		mTextView = (TextView) findViewById(R.id.textview);
	}

	public void onClick(View view) {
		//连接.net代码
		try {
			String serviceUrl = "http://192.168.3.178/WebTest/Service1.asmx";
			// 方法名
			String methodName = "UPlOAD";
			//命名空间名
			String namespace = "http://tempuri.org/";
			/*
			 * "http://tempuri.org/" 为命名空间,命名空间是.net默认的
			 * 通过SoapObject类 创建一个对象,以命名空间和要访问的方法名构造
			 */
			SoapObject request = new SoapObject(namespace,
					methodName);
			//添加传递的参数名称及值,这是城的参数名称是任意的,可以不与服务上的方法名称一至
			request.addProperty("name", "23");
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);
//			envelope.bodyOut = request;
			HttpTransportSE htp = new HttpTransportSE(serviceUrl);
//			AndroidHttpTransport httpTransport = new AndroidHttpTransport(serviceUrl);
			// 要访问的命名空间+方法名,也就是别人说的ACTION
			htp.call(namespace+methodName, envelope);
//			httpTransport.call(null, envelope);
//			SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
			String result = envelope.getResponse().toString();
			ShowDialog(result);
		} catch (Exception e) {
			e.printStackTrace();
			ShowDialog(e.getMessage());
		}

		/*
		 * 连接Java代码 String serviceUrl =
		 * "http://192.168.1.236:8080/axis2/services/SimpleServer?wsdl"; String
		 * methodName = "getGreeting"; SoapObject request = new
		 * SoapObject("http://ws.apache.org/axis2",methodName);
		 * request.addProperty("name",mEditText.getText().toString());
		 * SoapSerializationEnvelope envelope = new
		 * SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut =
		 * request; HttpTransportSE ht = new HttpTransportSE(serviceUrl); try{
		 * ht.call(null, envelope); if(envelope.getResponse() != null)
		 * {//ShowDialog("连接服务");
		 * mTextView.setText(envelope.getResponse().toString()); }
		 * }catch(Exception e){
		 * 
		 * }
		 */
	}

	public void ShowDialog(String msg) {
		new AlertDialog.Builder(this).setTitle("提示").setMessage(msg)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).show();
	}
}

?我用的是.net服务,我对.net不太董,在这里就不写了,可以去参考别人的代码

在上面的命名空间,方法名,Action等信息都可以在我们用连接打开页面中找到,其中Action在进入方法中才能看到

?

1 楼 ouyangtianhan 2011-09-14  
谢谢,学习了。
请教个问题:我的也是用的KSoap2,wsdl是自己写的。但是,当执行完之后,总是报错说:Method “...” not implemented!
而如果我,给MethodName赋值为“types”中的“element”标签的name的时候,就可以得到response。
请教这是为什么呢》????
谢谢先
2 楼 hilary3113 2011-09-15  
ouyangtianhan 写道
谢谢,学习了。
请教个问题:我的也是用的KSoap2,wsdl是自己写的。但是,当执行完之后,总是报错说:Method “...” not implemented!
而如果我,给MethodName赋值为“types”中的“element”标签的name的时候,就可以得到response。
请教这是为什么呢》????
谢谢先

这我也不太清楚,只能自己多调试了
  相关解决方案