如何在Android中调用Web服务? [英] How To Call Web Service In Android ?

查看:86
本文介绍了如何在Android中调用Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码这不起作用

Here Is My Code Below "This doesn't work"

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Mainactivity extends Activity {

	private Button ButtonCall;

	  private static String SOAP_ACTION1 = "http://tempuri.org/HelloWorld";
		private static String SOAP_ACTION2 = "http://tempuri.org/Ring";
		private static String NAMESPACE = "http://tempuri.org/";
	    private static String METHOD_NAME1 = "HelloWorld";
		private static String METHOD_NAME2 = "Ring";
		private static String URL = "http://mobiletest.somee.com/Service1.asmx?WSDL";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_service);
		
		
		ButtonCall = (Button)findViewById(R.id.button1);
		ButtonCall.setOnClickListener(new OnClickListener() {
			
			private TextView CallingText;

			public void onClick(View arg0) {
				//Initialize soap request + add parameters
		    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);        
		    	
		    	//Use this to add parameters
		    	request.addProperty("Ringu","Ring");
		    	
		    	//Declare the version of the SOAP request
		    	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
		    	
		    	envelope.setOutputSoapObject(request);
		    	envelope.dotNet = true;
		    	
		    	
		    	try {
		    		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		    		
		    		//this is the actual part that will call the webservice
		    		androidHttpTransport.call(SOAP_ACTION2, envelope);
		    		
		    		// Get the SoapResult from the envelope body.
		        	SoapObject result = (SoapObject)envelope.bodyIn;

		        	if(result != null)
		        	{
		        		//Get the first property and change the label text
		        		CallingText = (TextView)findViewById(R.id.TEXTVIEW1);
		        		CallingText.setText(result.getProperty(0).toString());
			    	}
		        	else
		        	{
		        		Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
		        	}
		    	} catch (Exception e) {
		    		e.printStackTrace();
		    	}
			}
		});
	
 
}

推荐答案

如何在Android中调用Web服务?

看这里:

从Android访问Web服务的逐步方法 [ ^ ]

如何使用SOAP在Android中调用Web服务 [ ^ ]



如需更多信息,请查看这里。 [ ^ ]
How To Call Web Service In Android ?
Look here:
Step by Step Method to Access Webservice from Android[^]
How to Call Web Service in Android Using SOAP[^]

For more, look here.[^]


检查此链接:



how-to-create-and-consume-net-wbeservice [ ^ ]


这篇关于如何在Android中调用Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆