如何在Android中调用Web服务

如何在Android中调用Web服务

本文介绍了如何在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服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 20:49