Basically I am using kSoap2 classes for Android. The client app is consuming a web service. If required I can post the code.
I surfed around to figure this out. I get following error message
expected: START_TAG{http://schemas.xmlsoap.org/envelope/}Envelope(posiiton:START_TAG#1:6 in java.io.InputStreamReader#437b40f0) when androidHttpTransport.call(SOAP_ACTION, envelope); is called.
What I gathered so far is "The xml is not parsed correctly or there was no response from the webservice server." Any suggestions?
Also, When I tried other external webservice: http://www.deeptraining.com/webservices/GetWeather, I received: IOException on androidHttpTransport.call(SOAP_ACTION, envelope);
IOException message: Host is unresolved, any idea how to solve this, here I didn't get the XmlPullParserException (may be it will show up once this is fixed).
Thanks and I appreciate your insight and time.
Anjana.
i had the same problem and i resolved it. I only wrote a wrong URL.
I have a php web service using NUSOAP.
Wrong address:
private static final String URL = "http://www.mywebsite.com/service/myservice.php?wsld";
Right address:
private static final String URL = "http://www.mywebsite.com/service/myservice.php";
I advise you to check the parameters you are passing to ksoap2, because it works fine.
Bye bye.
Related
I am experiencing what I find to be a VERY strange problem currently. I have database with about 1000 records. I have a web service for fetching these and turning them into a json string. Testing this using soapui or another web service client works perfectly fine.
My problem is that when I fetch the data using ksoap2 in android it crashes, and here is the crazy part: it only crashes sometimes!?!?
After quite a bit of debugging I have noticed that the JSON i get in android is ALMOST the same as the one I get in eg. SOAPUI.
The problems I have seen are
it has removed a ":" between a key and value- eg. "id""2" instead of "id":"2"
it might be missing a '"' on either side of a key or a value. eg: "id:"2" instead of "id":"2"
An entire key has been removed, eg: "name":"myname","2" instead of "name":"myname","id":"2". Furthermore it is NOT the same place that something is removed every time...
I am seeing these errors (when I use logs) before I start parsing the json (which of course fails if it is not valid json). Ss the web service is returning the correct thing (according to SOAPUI) I'm assuming it is ksoap that somehow messes up parsing the soap response.
Keep in mind as well that it occasionally works and the json has all the data and is successfully parsed!
Here is my code calling the web service (The json is ~345.000 characters):
SoapObject request = new SoapObject(NAMESPACE, method);
input.addPropsToRequest(request);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.dotNet = false;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(soap_action, envelope);
String json = envelope.getResponse().toString();
I have a WSDL java based webservice and an android project.I didnt get any response from that wsdl url.
I couldn't solve this problem.
Here is Android Code Part.
try
{
SoapObject request = new SoapObject( "http://keycloud.noip.me:8081/knowledgecenterservice/webservice/ContentManagementService" , "getImageFromDistrict " );
request.addProperty( "district" , "Bangalore" );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11 );
new MarshalBase64().register( envelope ); //serialization
envelope.encodingStyle = SoapEnvelope.ENC;
envelope.setOutputSoapObject( request );
HttpTransportSE transport = new HttpTransportSE( "http://keycloud.noip.me:8081/knowledgecenterservice/webservice/ContentManagementService" );
transport.call( "http://keycloud.noip.me:8081/knowledgecenterservice/webservice/ContentManagementService/getImageFromDistrict" , envelope );
SoapObject value = ( SoapObject ) envelope.getResponse();
text.setText(value.toString());
dialog.dismiss();
}catch(Exception e)
{
}
Try changing
androidHttpTransport.call("", envelope);
to
androidHttpTransport.call(METHOD_NAME, envelope);
To check the response from server
Install SOA Client Add-on on Mozilla Firefox.
I checked the response from server using the above specified URL i.e
http://keycloud.noip.me:8081/ekrishi/webservice/TestService?wsdl
and I happen to see that the method is not requesting for any parameters/inputs. So I don't know why did you wirte this line request.addProperty("district","Bangalore");
And I just invoked that method without any parameters and I got the error
faultcode: soap:Server -->means there's a problem on server side. So check with the person who has written this webservice.
faultstring: Fault occurred while processing.
Hope this information is useful for you :)
EDIT I have attached the snapshot of the error I got using SOA client. If everything is fine on server side, we will not get any error. I strongly believe that there's a problem on server side. Check your URL once.
See here the method is not requesting for any parameters. So no need to add -- request.addProperty("district", "Bangalore");. Below is the screenshot of that.
I feel like the URL you are using in wrong. Cross check it once.
i've got a problem do a http post on android with german "umlaute" äöü. I am passing a json object to the method below and execute the returned ClientResource with post and the request entity in the returned client response. When I want to post something like { "foo":"bär" } the HttpClient sends something like { "foo":"b√§r" }.
Don't know why. What am I doing wrong.
public static ClientResource newPostRequest(Context context, String urn,
JSONObject form) throws MissingAccessTokenException {
ClientResource resource = new ClientResource(uri + urn);
StringRepresentation sr = new StringRepresentation(form.toString());
sr.setMediaType(MediaType.APPLICATION_JSON);
resource.getRequest().setEntity(sr);
return resource;
}
Update
I used the default android http client (which is a apache http client I believe) and got the same error. So the problem might be located here. I try to implement another json parser (currently gson) and (if possible) another http client. Be back later...
Update
Gson is not the problem. I added a json String to the StringRepresentation and nothing changed.
ANSWER
Well that's an odd one. Maybe someone can clear this for me. I always asked myself, why √§ where used and I figured out that translating the utf-8 ä leads to √§. Obviously my android phone did not use macroman, but my mac did. I therefor changed the text file encoding in eclipse, restarted eclipse and the tomcat server and it worked. Still the TCP/IP Monitor in eclipse uses mac roman which looks still wrong. It was thereby a problem with my server, not with the restlet client on android. I just couldn't see it because the TCP/IP Monitor encoded everything in macroman.
did you try calling setCharacterSet(...) on your StringRepresentation? e.g.,
StringRepresentation sr = new StringRepresentation(form.toString());
sr.setCharacterSet(CharacterSet.UTF_8);
I'm developing an Android app which needs to connect to a .NET SOAP web service and generate/populate a number of objects from the response. I'm totally new to web services and SOAP, and relatively new to Android. The web service has already been built (not by me).
I've been trying to find info on connecting to SOAP web services in Android. The two basic suggestions seem to be:
Don't use SOAP,
If you do use SOAP, use KSOAP2.
I've looked at various tutorials for KSOAP2 but they all seem to deal only with the most basic, primitive types, such as sending 2 ints to get 1 int back, or sending and receiving strings;, however, I need to send custom objects back and forth.
Is it possible to send and receive custom objects using KSOAP2? If so, how easy/difficult is this? Does the XML have to be parsed to create/populate the objects "by hand"?
EDIT/UPDATE:
After trying for a while to connect to the existing webservice (takes a String and returns a complex object), and getting an error, I decided to try a simpler approach. I connected to a simpler webservice which takes no parameters, and returns an int. This simpler webservice is hosted in the same place as the original one, and the simple one now works fine for me.
FURTHER UPDATE:
All working fine now! Turns out I just had a capitalisation error which was throwing off my parameter.
The final problem was "national" versus "National". Here's the soapUI-generated code:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetClientByNationalID>
<!--Optional:-->
<tem:nationalID>XXXXXXX</tem:nationalID>
</tem:GetClientByNationalID>
</soapenv:Body>
</soapenv:Envelope>
And the request code which was being generated by my Java:
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<GetClientByNationalID xmlns="http://tempuri.org/" id="o0" c:root="1">
<NationalID i:type="d:string">
XXXXXXXX
</NationalID>
</GetClientByNationalID>
</v:Body>
</v:Envelope>
My final java code is:
public void webServiceCall() {
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "GetClientByNationalID";
String SOAP_ACTION = "http://tempuri.org/IClientService/GetClientByNationalID";
// unsecure service
String URL = "http://XXXXXXXXXXXXXXXXXXXX.net/FPUnSecureService/ClientService.svc";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String clientID = "XXXXXXX";
PropertyInfo pi = new PropertyInfo();
pi.setName("nationalID");
pi.setValue(clientID);
pi.setType(clientID.getClass());
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "GetClientByNationalID", new ClientID().getClass());
Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);
HttpTransportSE t = new HttpTransportSE(URL);
ClientID client = new ClientID();
t.debug = true;
try {
Log.i("webServiceCall", "Trying call to web service");
t.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
Log.i("success", response.getPropertyAsString(0));
Log.i("success", response.getPropertyAsString(1));
Log.i("success", response.getPropertyAsString(2));
Log.i("success", response.getPropertyAsString(3));
Log.i("success", response.getPropertyAsString(4));
} catch (Exception e) {
Log.e("webServiceCall", "Error calling webservice.");
e.printStackTrace();
System.out.println("Request: " + t.requestDump);
System.out.println("Response: " + t.responseDump);
}
}
I am still confused about these lines:
envelope.addMapping(NAMESPACE, "GetClientByNationalID", new ClientID().
Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);
I think I added the Marshal parts when I was passing an object as a parameter, I'm not sure if I still need them. I'm also not sure about the addMapping call either, and what I should have in there. Thanks.
Use ksoap2, and check my answers with code examples at the following links: Link1,link2,link3. I wrote some details which will help you understand how to start coding.
let me know if u need help.
UPDATE ( Answering your question about mapping and marshalling)
u use addMapping when you are Sending a complex type ( ie an object) through the soap envelope. How will the webservice recognize the complex type ? u need to create locally a class that implement kvmserializable (as mentionned in my links) which will have same parameter as the object on the server, and then u need to add mapping between it and the class that maps to it on the server, so that the enveloppe when parsed on the server it knows this complex type X map to class X on the server. So if you are not Sending complex type, you don't need to add mapping.(PS:I see that your not sending a complex type since nationalID is of type string. IF lets say nationalID was a complex type of type Z you would do : addMapping(NAMESPACE, Z.class.getSimpleName(), Z.class) )
As for marshalling, it uses java serialization to change Objects to stream of data to be unmarshalled on the web service. So when u are sending a complex type,based on my experience, it is a good practice to add marshalling to change the complex type to streams of data by serializing it. If you find that you don't need it just don't add it, but its always good to understand what are the options out there.
Look at this http://seesharpgears.blogspot.com/2010/10/ksoap-android-web-service-tutorial-with.html , there are examples of how to pass complex objects.
I want to access a Restful web service. I want the request should be in the following format.
GET /API/Contacts/username HTTP/1.1
HOST: $baseuri:port
Accept: text/xml
Authorization: Basic ZmF0aWdhYmxlIGdlbmVyYXR=
And also I am calling the web service though HTTPS protocol.
The folowing is the code I am using :
HttpGet get = new HttpGet("https://secure.myapp.com/MyApp/API/Contacts/myname");
get.addHeader("Accept","text/xml");
get.addHeader("Authorization","Basic ZmF0aWdhYmxlIGdlbmVyYXR=");
get.addHeader("Host","https://secure.myapp.com");
get.addHeader("Connection Use","HTTP 1.1");
DefaultHttpClient client = new DefaultHttpClient();
ResponseHandler objHandler = new BasicResponseHandler();
String getResponse = client.execute(get,objHandler);
But I am getting an Error : 400 Bad request.
I am not sure whether my code is correct. Is it necessary to specify the method (GET, POST or PUT) explicitly in the header?
Please help me...
Thnking You....
Shouldn't you put 'Basic ZmF0aWdhYmxlIGdlbmVyYXR=' in double quotes?
Also check web service example request and response and make sure that everything is specified.
Ohh...
That was my mistake.
I did not keep the order of adding headers.
When I changed its order according to the Request it is working fine.
In the request the HOST should be the first parameter.
The following is the corrected code.
get.addHeader("Host","secure.myapp.com");
get.addHeader("Accept","text/xml");
get.addHeader("Authorization","Basic ZmF0aWdhYmxlIGdlbmVyYXR=");
get.addHeader("Connection Use","HTTP 1.1");
Sorry to disturb you all...