Android:使用KSOAP解决Web服务问题 [英] Android: Problem Consuming Web Service with KSOAP
问题描述
我正在尝试使用这个网络服务:http://www.ezzylearning.com/services/CountryInformationService.asmx with KSoap。
当我打电话没有参数的web方法(例如:GetContinents)可以正常工作。当我尝试使用带参数的一个时,它总是返回空数据。
这是我的Activity类(用于测试目的):
公共类MyTestActivity扩展Activity
{
private static final String NAMESPACE =http://www.ezzylearning.com/services/;
private static final String URL =http://www.ezzylearning.com/services/CountryInformationService.asmx?WSDL;
private static final String ACTION_GET_COUNTRIES_BY_CONTINENT =http://www.ezzylearning.com/services/CountryInformationService.asmx/GetCountriesByContinent;
private static final String METHOD_GET_COUNTRIES_BY_CONTINENT =GetCountriesByContinent;
/ **首次创建活动时调用。 * /
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);
String property =continentCode;
字符串值=AS;
new GetCountriesByContinentTask()。execute(URL,NAMESPACE,ACTION_GET_COUNTRIES_BY_CONTINENT,METHOD_GET_COUNTRIES_BY_CONTINENT,property,value);
}
//使用AsyncTask的实现按大陆分类。
私有类GetCountriesByContinentTask扩展AsyncTask< String,Void,String>
{
@Override
protected void onPreExecute()
{
}
@Override
protected String doInBackground(String ... params)
{
String result =Testing ...;
String url = params [0];
String namespace = params [1];
String action = params [2];
String method = params [3];
String property = params [4];
String value = params [5];
List< PropertyInfo> propertyList = new ArrayList< PropertyInfo>();
//使用此方法添加参数
PropertyInfo pi = new PropertyInfo();
pi.setName(property);
pi.setValue(value);
pi.setType(String.class);
propertyList.add(pi);
尝试
{
MySoapHandler soapHandler = new MySoapHandler();
SoapObject soapRresult = soapHandler.soapCall(url,namespace,action,method,propertyList);
if(soapRresult!= null)
{
System.out.println(RESULT:+ soapRresult.toString()); //数据集总是空的
}
}
catch(exception ex)
{
System.out.println(Exception:+ ex.getMessage ());
ex.printStackTrace();
}
返回结果;
}
@Override
protected void onPostExecute(String result)
{
}
}
}
我的拨打网络服务的课程:
public class MySoapHandler
{
public SoapObject soapCall(String url,
String namespace,
String action,
String method,
List< PropertyInfo> propertyInfoList)
抛出IOException,XmlPullParserException
{
//初始化soap请求
SoapObject request = new SoapObject(namespace,method);
//添加参数
if(propertyInfoList!= null)
{
for(PropertyInfo pi:propertyInfoList)
{
request。方法addProperty(PI);
}
}
//声明SOAP请求的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
//调用webservice
HttpTransportSE transport = new HttpTransportSE(url);
transport.call(action,envelope);
//从信封体返回SoapResult。
return(SoapObject)envelope.bodyIn;
}
}
我知道我做错了什么?
我也尝试将envelope.bodyIn更改为envelope.getResponse(),但数据集仍为空。
谢谢。
package com.example.ss;
import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.app.ProgressDialog;
import android.widget.TextView;
import android.widget.Toast;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
/ * *
*由seguro6于6创建/ 14/13。
* /
public class sampletest extends 活动{
private static final 字符串 SOAP_ACTION = http://www.ezzylearning.com/services/CountryInformationService.asmx/GetCountriesByContinent跨度>;
private static final < span class =code-sdkkeyword> String METHOD_NAME = GetCountriesByContinent;
private static final < span class =code-sdkkeyword> String NAMESPACE = http://www.ezzylearning.com /services/CountryInformationService.asmx跨度>;
private static final < span class =code-sdkkeyword> String URL = http://www.ezzylearning.com /services/CountryInformationService.asmx跨度>;
对象响应;
TextView displayTextview;
public void onCreate(Bundle savedInstanceState){
超跨度> .onCreate(savedInstanceState);
displayTextview = new TextView( this );
setContentView(displayTextview);
new Dowork()。execute();
}
public class Dowork extends AsyncTask< string,object,object>
{
private final ProgressDialog dialog = new ProgressDialog(sampletest。此跨度>);
@覆盖
受保护 void onPreExecute()
{
this .dialogue.setMessage( 正在检查..);
此 .dialogue.show();
}
@覆盖
protected void onCancelled(对象结果)
{
super .onCancelled();
}
@覆盖
protected void onPostExecute(对象结果)
{
if (result!= null)
{
displayTextview.setText(result.toString());
}
else
{
Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_LONG )。节目();
}
if ( this .dialogue.isShowing()){
this .dialogue.dismiss();
}
}
@覆盖
protected 对象doInBackground( String ... params)
{
SoapObject request = new SoapObject (NAMESPACE,METHOD_NAME);
request.addProperty( continentCode, AF);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
尝试
{
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
aht.call(SOAP_ACTION,envelope);
request =(SoapObject)envelope.getResponse();
response = request.toString();
返回响应;
}
catch (Exception ex)
{
ex.printStackTrace();
}
返回响应;
}
}
}
查看此链接
消耗-.NET的web服务功能于机器人-usingksoap2 [< a href =http://articlesforprogramming.blogspot.in/2013/06/consume-net-webservice-in-android-using.htmltarget =_ blanktitle =New Window> ^ ]
I'm trying to consume this web service: http://www.ezzylearning.com/services/CountryInformationService.asmx with KSoap.
When I call a web method (example: GetContinents) without parameters it work fine. When I try to use one with parameters it always return empty data.
Here is my Activity class (for testing purposes):
public class MyTestActivity extends Activity { private static final String NAMESPACE = "http://www.ezzylearning.com/services/"; private static final String URL = "http://www.ezzylearning.com/services/CountryInformationService.asmx?WSDL"; private static final String ACTION_GET_COUNTRIES_BY_CONTINENT = "http://www.ezzylearning.com/services/CountryInformationService.asmx/GetCountriesByContinent"; private static final String METHOD_GET_COUNTRIES_BY_CONTINENT = "GetCountriesByContinent"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fourth); String property = "continentCode"; String value = "AS"; new GetCountriesByContinentTask().execute(URL, NAMESPACE, ACTION_GET_COUNTRIES_BY_CONTINENT, METHOD_GET_COUNTRIES_BY_CONTINENT, property, value); } // Implementation of AsyncTask used get the countries by continent. private class GetCountriesByContinentTask extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { } @Override protected String doInBackground(String... params) { String result = "Testing..."; String url = params[0]; String namespace = params[1]; String action = params[2]; String method = params[3]; String property = params[4]; String value = params[5]; List<PropertyInfo> propertyList = new ArrayList<PropertyInfo>(); //Use this to add parameters PropertyInfo pi = new PropertyInfo(); pi.setName(property); pi.setValue(value); pi.setType(String.class); propertyList.add(pi); try { MySoapHandler soapHandler = new MySoapHandler(); SoapObject soapRresult = soapHandler.soapCall(url, namespace, action, method, propertyList); if (soapRresult != null) { System.out.println("RESULT: " + soapRresult.toString()); // THE DATA SET IS ALWAYS EMPTY } } catch (Exception ex) { System.out.println("Exception: " + ex.getMessage()); ex.printStackTrace(); } return result; } @Override protected void onPostExecute(String result) { } } }
My Class for calling the web service:
public class MySoapHandler { public SoapObject soapCall(String url, String namespace, String action, String method, List<PropertyInfo> propertyInfoList) throws IOException, XmlPullParserException { //Initialize soap request SoapObject request = new SoapObject(namespace, method); //Add parameters if(propertyInfoList != null) { for(PropertyInfo pi: propertyInfoList) { request.addProperty(pi); } } //Declare the version of the SOAP request SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); envelope.dotNet = true; //Call the webservice HttpTransportSE transport = new HttpTransportSE(url); transport.call(action, envelope); //Return the SoapResult from the envelope body. return (SoapObject)envelope.bodyIn; } }
Any idea of what I'm doing wrong?
I also tried to change envelope.bodyIn to envelope.getResponse(), but the dataset is still empty.
Thank you.
package com.example.ss; import android.app.Activity; import android.os.Bundle; import android.os.AsyncTask; import android.app.ProgressDialog; import android.widget.TextView; import android.widget.Toast; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.AndroidHttpTransport; /** * Created by seguro6 on 6/14/13. */ public class sampletest extends Activity { private static final String SOAP_ACTION = "http://www.ezzylearning.com/services/CountryInformationService.asmx/GetCountriesByContinent"; private static final String METHOD_NAME = "GetCountriesByContinent"; private static final String NAMESPACE = "http://www.ezzylearning.com/services/CountryInformationService.asmx"; private static final String URL = "http://www.ezzylearning.com/services/CountryInformationService.asmx"; Object response; TextView displayTextview; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); displayTextview=new TextView(this); setContentView(displayTextview); new Dowork().execute(); } public class Dowork extends AsyncTask<string,object,object> { private final ProgressDialog dialogue =new ProgressDialog(sampletest.this); @Override protected void onPreExecute() { this.dialogue.setMessage("Checking.."); this.dialogue.show(); } @Override protected void onCancelled(Object result) { super.onCancelled(); } @Override protected void onPostExecute(Object result) { if(result!=null) { displayTextview.setText(result.toString()); } else { Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_LONG).show(); } if (this.dialogue.isShowing()) { this.dialogue.dismiss(); } } @Override protected Object doInBackground(String ...params) { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("continentCode","AF"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(request); try { AndroidHttpTransport aht=new AndroidHttpTransport(URL); aht.call(SOAP_ACTION,envelope); request=(SoapObject)envelope.getResponse(); response=request.toString(); return response; } catch (Exception ex) { ex.printStackTrace(); } return response; } } }
check this link
consume-.net-webservice-in-android-usingksoap2[^]
这篇关于Android:使用KSOAP解决Web服务问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!