Firstly thank you for all answers, this subject may be closed because I made what I want already. I mentioned about how to call a .NET webmethod below.
http://aaarkonusurum.blogspot.com/2011/10/android-ile-dataset-donen-bir-net.html
If you need only a method for calling a webservice method, use this:
public static SoapPrimitive callWebServiceMethod(String url,
String namespace, String methodName,
HashMap<String, Object> parameters, String soapAction)
throws IOException, XmlPullParserException, SoapFault {
SoapObject request = new SoapObject(namespace, methodName);
if (parameters != null) {
String[] keys = new String[0];
keys = (String[]) parameters.keySet().toArray(keys);
Object[] vals = (Object[]) parameters.values().toArray();
for (int i = 0; i < parameters.size(); i++) {
request.addProperty(keys[i], vals[i]);
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url,
60 * 1000);
androidHttpTransport.call(soapAction, envelope);
return (SoapPrimitive) envelope.getResponse();
}
and in your activity:
private static final String NAMESPACE = "http://tempuri.org/";
private static final String METHOD_NAME = "MethodName";
private static final String SOAP_ACTION = "http://tempuri.org/MethodName";
String url = "http://...";
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("param1", param1);
parameters.put("param2", param2);
//...
SoapPrimitive response = callWebServiceMethod(url,
NAMESPACE, METHOD_NAM, parameters,
SOAP_ACTION);
String webServiceResult = response.toString();
Related
I'm unable to figure this out. My code is throwing this error
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vector
.WebService.parseJourney
public static ArrayList<Journey> parseJourney(Object response) {
ArrayList<Journey> rs = new ArrayList<Journey>();
try {
if (response == null) {
return rs;
}
#SuppressWarnings("unchecked")
Vector<Object> result = (Vector<Object>) response;
if (result.size() < 4) {
return rs;
}
Am sure have used generics with no issues in past.
Wow - that was quick.
The call to parseJourney :
Vector<EntryValue> values = new Vector<EntryValue>();
EntryValue value = new EntryValue();
SoapObject request = new SoapObject(NAMESPACE_ENTRY, METHOD_NAME_ENTRY);
PropertyInfo pi = new PropertyInfo();
pi.setName("values");
pi.setValue(values);
pi.setType(MyArrayList.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE aht = new HttpTransportSE(URL_ENTRY);
try {
aht.call(SOAP_ACTION_ENTRY, envelope);
Object response = envelope.getResponse();
resultEntry = parseJourney(response);
return response.toString();
}
catch (Exception e) {
ERROR_EXCEPTION = 1;
return e.toString();
}
}
parseJourney() receives a String and you are trying to cast it to Vector without testing it
if(response instanceof Vector)
{
Vector<Object> result = (Vector<Object>) response;
//...
}
And don't put #SuppressWarnings("unchecked"), it's generally a bad practice
The Object that is being passed into parseJourney() is a String, not a Vector. If you want more information than that, you'll need to include whatever code calls parseJourney().
I develop an android application and i send a class object to a webservice method and i supposed to take an array as response but it returns anyType{}.
Here is a part of my code.
Customer C = new Customer();
C.setProperty(0,"30000001");
PropertyInfo pi =new PropertyInfo();
pi.setName("customer");
pi.setValue(C);
pi.setType(C.getClass());
request.addProperty(pi);
try{
androidHttpTransport.call(SOAP_ACTION1, envelope);
SoapObject response = (SoapObject) envelope.bodyIn;
String[] denemeList;
denemeList = new String[response.getPropertyCount()];
for(int i=0; i<response.getPropertyCount(); i++)
{
denemeList[i] = response.getPropertyAsString(i).toString();
Log.d("This is the response",denemeList[i]);
}
}catch (Exception e) {
TextView01.setText("EXCEPTION");
e.printStackTrace();
}
I find some other codes except from this one but none of them work.
Anyone know what should i do?
Thanks.
NOTE : HERE IS MY CLASS , I THINK IT WILL HELP YOU JUST POST tHIS
CLASS AS IT IS IN UR PROJECT 2. REPLACE THE VARIABLE ACCORDINGLY 3.
CALL METHOD : getDistrictDetials IN SUPPOSE MAIN CLASS...THIS WILL
PRINT THE OUTPUT IN CONSOL WINDOW
public class WebServiceCaller {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.MYSERVICE/Service.asmx";
private final String SOAP_ACTION = "http://tempuri.org/RASHTRWADI_State";
private final String METHOD_NAME = "RASHTRWADI_State";
public boolean getDistrictDetials() {
boolean result = false;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo propInfo1 = new PropertyInfo();
propInfo1.setName("State_Code");
propInfo1.setValue(1);
propInfo1.setType(int.class);
request.addProperty(propInfo1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true; // put this only if the web service is .NET one
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
// SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Object response1 = envelope.getResponse();
SoapObject response = (SoapObject) response1;
Log.i("myApp", response1.toString());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
I am trying to consume a JAVA web service from android.
Here is what I have tried so far:
private void CallWebServiceDummy() {
// TODO Auto-generated method stub
try {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
soapEnvelope.dotNet = false;
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
StringArraySerializer a = new StringArraySerializer();
a.add("hello"); a.add("world"); String n0 = NAMESPACE;
pi = new PropertyInfo(); pi.setName("a"); pi.setValue(a);
pi.setType(a.getClass()); pi.setNamespace(n0);
Request.addProperty(pi);
String b = "my name"; pi = new PropertyInfo(); pi.setName("b");
pi.setValue(b); Request.addProperty(pi);
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
Log.d("test", "request: " + androidHttpTransport.requestDump);
Log.d("test", "response: " + androidHttpTransport.responseDump);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
String c = resultsRequestSOAP.toString();
} catch (Exception e) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, e.getMessage(), duration);
toast.show();
toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
}
}
My Java web service Code:
package MyPackage;
public class WebServiceClass {
public String addnumbers(String[] a, String b) {
String c = new StringBuilder("This the String1 ").append(a[0]).append(" merged with String2 ").append(b).toString();
return c;
}
}
My Globals:
private static final String NAMESPACE = "http://MyPackage"; private
static final String URL =
"http://10.0.2.2:8080/WebService/services/WebServiceClass?wsdl"; private
static final String SOAP_ACTION = "urn:addnumbers"; private static final
String METHOD_NAME = "addnumbers";
Issue:
The response I receive is:
addnumbersResponse{return=This the String1 merged with String2 my name; }
The first parameter is not being sent to web service. I have tried to remove this line:
soapEnvelope.dotNet = false; but its still not working.
Guys please help me out. I am stuck for two days. Thanks for any help provided.
Your method's first argument's type is String array, not only String. Instead of StringArraySerializer, please try adding strings manually. You should see this page for this: Adding an array of complex objects to the request.
Check if your namespace is right!
I spent three hours with the same problem and only then realized that the namespace that was declared in the WebService class was different from what I put in the creation of SoapObject.
I am using commons HttpClient to make an http call to a Spring servlet. I need to add a few parameters in the query string. So I do the following:
HttpRequestBase request = new HttpGet(url);
HttpParams params = new BasicHttpParams();
params.setParameter("key1", "value1");
params.setParameter("key2", "value2");
params.setParameter("key3", "value3");
request.setParams(params);
HttpClient httpClient = new DefaultHttpClient();
httpClient.execute(request);
However when i try to read the parameter in the servlet using
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getParameter("key");
it returns null. In fact the parameterMap is completely empty. When I manually append the parameters to the url before creating the HttpGet request, the parameters are available in the servlet. Same when I hit the servlet from the browser using the URL with queryString appended.
What's the error here? In httpclient 3.x, GetMethod had a setQueryString() method to append the querystring. What's the equivalent in 4.x?
Here is how you would add query string parameters using HttpClient 4.2 and later:
URIBuilder builder = new URIBuilder("http://example.com/");
builder.setParameter("parts", "all").setParameter("action", "finish");
HttpPost post = new HttpPost(builder.build());
The resulting URI would look like:
http://example.com/?parts=all&action=finish
If you want to add a query parameter after you have created the request, try casting the HttpRequest to a HttpBaseRequest. Then you can change the URI of the casted request:
HttpGet someHttpGet = new HttpGet("http://google.de");
URI uri = new URIBuilder(someHttpGet.getURI()).addParameter("q",
"That was easy!").build();
((HttpRequestBase) someHttpGet).setURI(uri);
The HttpParams interface isn't there for specifying query string parameters, it's for specifying runtime behaviour of the HttpClient object.
If you want to pass query string parameters, you need to assemble them on the URL yourself, e.g.
new HttpGet(url + "key1=" + value1 + ...);
Remember to encode the values first (using URLEncoder).
I am using httpclient 4.4.
For solr query I used the following way and it worked.
NameValuePair nv2 = new BasicNameValuePair("fq","(active:true) AND (category:Fruit OR category1:Vegetable)");
nvPairList.add(nv2);
NameValuePair nv3 = new BasicNameValuePair("wt","json");
nvPairList.add(nv3);
NameValuePair nv4 = new BasicNameValuePair("start","0");
nvPairList.add(nv4);
NameValuePair nv5 = new BasicNameValuePair("rows","10");
nvPairList.add(nv5);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
URI uri = new URIBuilder(request.getURI()).addParameters(nvPairList).build();
request.setURI(uri);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output .... ");
String respStr = "";
while ((output = br.readLine()) != null) {
respStr = respStr + output;
System.out.println(output);
}
This approach is ok but will not work for when you get params dynamically , sometimes 1, 2, 3 or more, just like a SOLR search query (for example)
Here is a more flexible solution. Crude but can be refined.
public static void main(String[] args) {
String host = "localhost";
String port = "9093";
String param = "/10-2014.01?description=cars&verbose=true&hl=true&hl.simple.pre=<b>&hl.simple.post=</b>";
String[] wholeString = param.split("\\?");
String theQueryString = wholeString.length > 1 ? wholeString[1] : "";
String SolrUrl = "http://" + host + ":" + port + "/mypublish-services/carclassifications/" + "loc";
GetMethod method = new GetMethod(SolrUrl );
if (theQueryString.equalsIgnoreCase("")) {
method.setQueryString(new NameValuePair[]{
});
} else {
String[] paramKeyValuesArray = theQueryString.split("&");
List<String> list = Arrays.asList(paramKeyValuesArray);
List<NameValuePair> nvPairList = new ArrayList<NameValuePair>();
for (String s : list) {
String[] nvPair = s.split("=");
String theKey = nvPair[0];
String theValue = nvPair[1];
NameValuePair nameValuePair = new NameValuePair(theKey, theValue);
nvPairList.add(nameValuePair);
}
NameValuePair[] nvPairArray = new NameValuePair[nvPairList.size()];
nvPairList.toArray(nvPairArray);
method.setQueryString(nvPairArray); // Encoding is taken care of here by setQueryString
}
}
This is how I implemented my URL builder.
I have created one Service class to provide the params for the URL
public interface ParamsProvider {
String queryProvider(List<BasicNameValuePair> params);
String bodyProvider(List<BasicNameValuePair> params);
}
The Implementation of methods are below
#Component
public class ParamsProviderImp implements ParamsProvider {
#Override
public String queryProvider(List<BasicNameValuePair> params) {
StringBuilder query = new StringBuilder();
AtomicBoolean first = new AtomicBoolean(true);
params.forEach(basicNameValuePair -> {
if (first.get()) {
query.append("?");
query.append(basicNameValuePair.toString());
first.set(false);
} else {
query.append("&");
query.append(basicNameValuePair.toString());
}
});
return query.toString();
}
#Override
public String bodyProvider(List<BasicNameValuePair> params) {
StringBuilder body = new StringBuilder();
AtomicBoolean first = new AtomicBoolean(true);
params.forEach(basicNameValuePair -> {
if (first.get()) {
body.append(basicNameValuePair.toString());
first.set(false);
} else {
body.append("&");
body.append(basicNameValuePair.toString());
}
});
return body.toString();
}
}
When we need the query params for our URL, I simply call the service and build it.
Example for that is below.
Class Mock{
#Autowired
ParamsProvider paramsProvider;
String url ="http://www.google.lk";
// For the query params price,type
List<BasicNameValuePair> queryParameters = new ArrayList<>();
queryParameters.add(new BasicNameValuePair("price", 100));
queryParameters.add(new BasicNameValuePair("type", "L"));
url = url+paramsProvider.queryProvider(queryParameters);
// You can use it in similar way to send the body params using the bodyProvider
}
Im using Java 8 and apache httpclient 4.5.13
HashMap<String, String> customParams = new HashMap<>();
customParams.put("param1", "ABC");
customParams.put("param2", "123");
URIBuilder uriBuilder = new URIBuilder(baseURL);
for (String paramKey : customParams.keySet()) {
uriBuilder.addParameter(paramKey, customParams.get(paramKey));
}
System.out.println(uriBuilder.build().toASCIIString()); // ENCODED URL
System.out.println(uriBuilder.build().toString); // NORMAL URL
Full example with DTO
public class HttpResponseDTO {
private Integer statusCode;
private String body;
private String errorMessage;
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
/**
*
* #param destinationURL
* #param params
* #param headers
* #return HttpResponseDTO
*/
public static HttpResponseDTO get(String baseURL, Boolean encodeURL, HashMap<String, String> params, HashMap<String, String> headers) {
final HttpResponseDTO httpResponseDTO = new HttpResponseDTO();
// ADD PARAMS IF
if (params != null && Boolean.FALSE.equals(params.isEmpty())) {
URIBuilder uriBuilder;
try {
uriBuilder = new URIBuilder(baseURL);
for (String paramKey : params.keySet()) {
uriBuilder.addParameter(paramKey, params.get(paramKey));
}
// CODIFICAR URL ?
if (Boolean.TRUE.equals(encodeURL)) {
baseURL = uriBuilder.build().toASCIIString();
} else {
baseURL = uriBuilder.build().toString();
}
} catch (URISyntaxException e) {
httpResponseDTO.setStatusCode(500);
httpResponseDTO.setErrorMessage("ERROR AL CODIFICAR URL: " + e.getMessage());
return httpResponseDTO;
}
}
// HACER PETICION HTTP
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
final HttpGet get = new HttpGet(baseURL);
// ADD HEADERS
if (headers != null && Boolean.FALSE.equals(headers.isEmpty())) {
for (String headerKey : headers.keySet()) {
get.setHeader(headerKey, headers.get(headerKey));
}
}
try (CloseableHttpResponse response = httpClient.execute(get);) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
httpResponseDTO.setBody(EntityUtils.toString(httpEntity));
httpResponseDTO.setStatusCode(response.getStatusLine().getStatusCode());
}
} catch(Exception e) {
httpResponseDTO.setStatusCode(500);
httpResponseDTO.setErrorMessage(e.getMessage());
return httpResponseDTO;
}
} catch(Exception e) {
httpResponseDTO.setStatusCode(500);
httpResponseDTO.setErrorMessage(e.getMessage());
return httpResponseDTO;
}
return httpResponseDTO;
}
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
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.
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?
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.
Calling webservice by passing parameters from j2me
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();
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.
Inside the WebService file in .net C#:
[WebService(Namespace = "http://something/webservice/v1")]
[WebMethod]
public DateTime[] Function(Guid organizationId, Guid categoryId)
{
return ...;
}
Inside the Android code:
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.
Try commenting out the line:
envelope.dotNet=true;
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.
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);
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);
}
}