Java 如何使用 ksoap2 将参数传递给 Web 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2108527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameter to a webservice using ksoap2?
提问by
I'm using Eclipse IDE to develop an android app. I'm trying to connect to a .net webservice. I'm using ksoap2 version 2.3
我正在使用 Eclipse IDE 开发一个 android 应用程序。我正在尝试连接到 .net 网络服务。我正在使用 ksoap2 2.3 版
When I'm calling a webmethod with no parameters, it works fine. When I come to pass a parameter to the webmethod, I get null (while debugging the webservice I discovered that) and I get a null from the webmethod in the client side code.
当我调用没有参数的 webmethod 时,它工作正常。当我将参数传递给 webmethod 时,我得到了 null(在调试 webservice 时我发现了这一点),并且我从客户端代码中的 webmethod 得到了 null。
Code:
代码:
package com.examples.hello;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://Innovation/HRService/stringBs";
private static final String METHOD_NAME = "stringBs";
private static final String NAMESPACE = "http://Innovation/HRService/";
private static final String URL = "http://196.205.5.170/mdl/hrservice.asmx";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
call();
}
public void call()
{
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//PropertyInfo PI = new PropertyInfo();
//request.addProperty("a", "myprop");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet=true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
String results = result.toString();
tv.setText( ""+results);
} catch (Exception e) {
tv.setText(e.getMessage());
}
}
}
Why do I get the null response, how do I pass a parameter to a webservice using ksoap2?
为什么我得到空响应,如何使用 ksoap2 将参数传递给网络服务?
回答by jaseem Ambalangadan
Calling webservice by passing parameters from j2me
通过从 j2me 传递参数来调用 webservice
SoapObject request = new SoapObject("http://www.webserviceX.NET", "GetCitiesByCountry");
String soapAction = "http://www.webserviceX.NET/GetCitiesByCountry";
request.addProperty("CountryName", "india");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
HttpTransport ht = new HttpTransport("http://www.webservicex.net/globalweather.asmx");
ht.debug = true;
//System.err.println( ht.requestDump );
ht.call(soapAction,envelope);
System.out.println("####################: " +envelope.getResponse());
//SoapObject result = (SoapObject)envelope.getResponse();
回答by Chris
Instead of
代替
request.addProperty("a", "myprop");
try using
尝试使用
request.addProperty("arg0", "myprop");
I'm not an expect on ksoap2 but i'm pretty sure this sets the value of the first parameter to your web service function. Has worked perfectly for me.
我不是对 ksoap2 的期望,但我很确定这将第一个参数的值设置为您的 Web 服务功能。对我来说非常有效。
回答by DanneJaha
I have been working with this for 2 days now and i finally got the solution. I submit my complete code and hope this will help. It Can pass Parameters and get response.
我已经为此工作了 2 天,我终于得到了解决方案。我提交了我的完整代码,希望这会有所帮助。它可以传递参数并获得响应。
Inside the WebService file in .net C#:
在 .net C# 中的 WebService 文件中:
[WebService(Namespace = "http://something/webservice/v1")]
[WebMethod]
public DateTime[] Function(Guid organizationId, Guid categoryId)
{
return ...;
}
Inside the Android code:
在 Android 代码中:
private final static String URL = "http://something/WebServices/WebService.asmx";
private final static String NAMESPACE = "http://something/webservice/v1";
public ArrayList<Object> getSoapObject(String METHOD_NAME, String SOAP_ACTION, Map<String, String> parameters){
try {
ArrayList<Object> sol = new ArrayList<Object>();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
if(parameters != null){
for (Entry<String, String> para : parameters.entrySet()) {
request.addProperty(para.getKey(), para.getValue());
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
Log.d("Body", envelope.bodyOut.toString());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.getResponse();
for(int i = 0; i < result.getPropertyCount(); i++){
sol.add(result.getProperty(i));
}
return sol;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void getMenuEndDate(String orgId, String categoryId){
Date startDate = null;
Date endDate = null;
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("organizationId", orgId);
parameters.put("categoryId", categoryId);
ArrayList<Object> sol = getSoapObject("Function", "http://something/webservice/v1/Function", parameters);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
try {
startDate = (Date)dateFormatter.parse(sol.get(0).toString());
endDate = (Date)dateFormatter.parse(sol.get(1).toString());
} catch (ParseException e) {
Log.d(TAG, "Exception i Date-Formatering");
e.printStackTrace();
}
}
Things to check:
检查事项:
- Are the parameters named exactly the same as what is expected in the Web Service?
- Check if you use Trailing "/" for the Namespace. Have the same in you application.
- 命名的参数是否与 Web 服务中的预期参数完全相同?
- 检查您是否对命名空间使用尾随“/”。在您的应用程序中具有相同的功能。
回答by Bishop
Try commenting out the line:
尝试注释掉该行:
envelope.dotNet=true;
信封.dotNet=真;
I did the same thing you did and when I read about this property being a really ugly hack, I commented it out for testing purposes and my parameter got passed correctly.
我做了和你一样的事情,当我读到这个属性是一个非常丑陋的黑客时,我为了测试目的将它注释掉并且我的参数被正确传递。
回答by Ye Yint Ko Ko
You will have to declare parameter type in client code:
您必须在客户端代码中声明参数类型:
SoapObject request = new SoapObject("http://tempuri.org/", "mymethod");
PropertyInfo p = new PropertyInfo();
p.setName("param_name_from_webservice");
p.setValue(true);
p.setType(Boolean.class);
request.addProperty(p);
回答by Chathura
In here Problem With the Order of Codes You Wrote, Don't Worry Try this, It's Worked for me.
在这里你写的代码顺序有问题,别担心试试这个,它对我有用。
private class ConversionAsyncTask extends AsyncTask<Void,Void,Void> {
private SoapPrimitive response;
protected Void doInBackground(Void... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("a","5");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.setOutputSoapObject(request);
soapEnvelope.dotNet = true;
soapEnvelope.implicitTypes = true;
try {
HttpTransportSE aht = new HttpTransportSE(URL);
aht.call(SOAP_ACTION, soapEnvelope);
response = (SoapPrimitive) soapEnvelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
temperatureTxt.setText("Status: " + response);
}
}