1

I am currently devoloping and app which is using ksoap2 web service. I am using "ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar"

Here is my problem.

I can succesfuly receive data on api10 and lower versions. (*on my phone and virtual machines)

But when ı tested my app on Android Virtual machines which is api 11 and higher versions.

HttpTransportSE does not work.

my codes;(not all of)

SoapSerializationEnvelope soapEnvol= new
SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE setConn = new HttpTransportSE(URL);

     try {
     setConn.call(SOAP_ACTION, soapEnvol);
    }
     catch{Exception e)
    {
          return null;
    }

""" setConn.call(SOAP_ACTION, soapEnvol); """ always falls to catch on api 11 av machines.

what i am missing? Thanks allready.

edit: (Full code respond)

public List<HashMap<String, String>> duyurulariListeOlarakGetir() {
        SoapObject istek = new SoapObject(NAMESPACE, METHOD_NAME);

        istek.addProperty("user", "123");
        istek.addProperty("pass", "123");
        istek.addProperty("appID", "1");

        SoapSerializationEnvelope soapZarf = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapZarf.dotNet = true;
        soapZarf.setOutputSoapObject(istek);

        HttpTransportSE baglantıKur = new HttpTransportSE(URL);

        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

        try {

            baglantıKur.debug=true;
            baglantıKur.call(SOAP_ACTION, soapZarf);

            SoapObject sonuclar = (SoapObject) soapZarf.getResponse();

            int totalCount = sonuclar.getPropertyCount();
            if (totalCount > 0) {
                for (int detailCount = 0; detailCount < totalCount; detailCount++) {

                    SoapObject pojoSoap = (SoapObject) sonuclar.getProperty(detailCount);
                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put("duyuruID", pojoSoap.getProperty("duyuruID").toString());
                    map.put("duyuruMetin", pojoSoap.getProperty("duyuruMetin").toString());
                    map.put("duyuruSaat", pojoSoap.getProperty("duyuruSaat").toString());
                    map.put("duyuruBaslik", pojoSoap.getProperty("duyuruBaslik").toString());
                    map.put("duyuruTarih", pojoSoap.getProperty("duyuruTarih").toString());
                    map.put("kurumID", pojoSoap.getProperty("duyuruSilindi").toString());
                    map.put("uygulamaID", pojoSoap.getProperty("duyuruSilindi").toString());
                    map.put("duyuruSilindi", pojoSoap.getProperty("duyuruSilindi").toString());

                    fillMaps.add(map);
                }
            } else
                return null;
        } catch (Exception e) {
            HashMap<String, String> map = new HashMap<String, String>();

            map.put("duyuruID", "");
            map.put("duyuruMetin", "Bağlantı kurulamadı");
            map.put("duyuruSaat", "Bağlantı kurulamadı");
            map.put("duyuruBaslik", "Bağlantı kurulamadı");
            map.put("duyuruTarih", "Bağlantı kurulamadı");
            map.put("kurumID", "Bağlantı kurulamadı");
            map.put("uygulamaID","Bağlantı kurulamadı");
            map.put("duyuruSilindi", "Bağlantı kurulamadı");

            fillMaps.add(map);
            return fillMaps;
        }
        return fillMaps;
    }
Mehmet Emre Portakal
  • 1,774
  • 21
  • 37
  • I think this is some sort of bug as i had also experienced this problem with my app running on ICS. If i'm not mistaken then it happens when you try to communicate with an SSL enabled server, rite? Well, eventually i had to switch to Json to resolve this and I'm very happy with this move as its more fast and easier to deal with – waqaslam Mar 15 '12 at 08:41
  • ı have to use kSoap because we are devoloping same app for Iphone and we want to use same web service. – Mehmet Emre Portakal Mar 15 '12 at 08:50
  • ı added full code but this codes works perfectly on under api version 10 – Mehmet Emre Portakal Mar 15 '12 at 08:54
  • Is your webservice in *ASP.NET*? because you can support both **xml** and **json** simultaneously just be changing a little in your **web.config**. My service is also supporting both and its working just perfect – waqaslam Mar 15 '12 at 08:56
  • @Waqas can you explain more? – Mehmet Emre Portakal Mar 15 '12 at 08:58
  • awesome... so if you want, you may simply enable support for Json too. Ask me if you need help with this :) – waqaslam Mar 15 '12 at 09:02
  • I mean Asp.NET webservices can support Json string response too. All you need to do is to add a **handler** in your web.config and decorate your service code file **[ScriptService]** attribute and you are done. Afterwards, if user request for xml in header of request, then the service will respond with xml/SOAP data. But if user mentions json in request header then Json string will be responded – waqaslam Mar 15 '12 at 09:05
  • I am using SOAP service in my iOS app with no problem, in case you want to use same web service for both Android and iOS, what @Waqas says might be useful -if I didn't misunderstand. – kubilay Mar 15 '12 at 09:14
  • 2
    Just for an overview, read this article: **http://www.codeproject.com/Articles/45275/Create-a-JSON-WebService-in-ASP-NET-2-0-with-a-jQu** It demonstrate both SOAP and Json responses from a single webservice. Personally, I would recommend to use Json as it requires less data to transfer the same information compared to SOAP, its simple, easy to grip and supported natively in Android. Moreover, you can read **http://stackoverflow.com/questions/2851117/scriptmethodresponseformat-responseformat-json** and **http://encosia.com/asmx-and-json-common-mistakes-and-misconceptions/** – waqaslam Mar 15 '12 at 09:20
  • @Waqas i have created a web service like your frist link. But how i create ı query for .net web service using json? – Mehmet Emre Portakal Mar 16 '12 at 15:53
  • first, are you able to see xml response and json response from same web-service in browser? – waqaslam Mar 16 '12 at 20:16
  • check out this link http://www.filedropper.com/jsondemo I've created a demo project in which I'm using your web-service to show how you can download data in json and recreate the objects using Gson. Ask me if you need any clarification or further help. Good luck :) – waqaslam Mar 17 '12 at 21:18
  • ohh yeah, moreover, if you want, you may actually tweak your web service a little to use even single method to offer both json and xml simultaneously (no need to write *TestJSON* and *TestXML*, just use *Test*). It will be helpful to keep your web service same (no duplicate methods) – waqaslam Mar 17 '12 at 21:24
  • @Waqas , Thank you a lot.your answers are very precious for me. and lastly i realy don't get consept of "single method to offer both json and xml simultaneously" this two method have diffirent return type. json method returns String. xlm method returns Entity[] array. – Mehmet Emre Portakal Mar 19 '12 at 08:30
  • Even if you remove the return type attribute at top of method and keep a single method (instead of two), it will still work. The return type is to restrict the response type, but we may let it depend on request type too - making it more dynamic to adapt the situation. Paste your web service code at pastebin.com and I'll modify it a little to show you an example :) – waqaslam Mar 19 '12 at 08:37
  • @Waqas THANKS A LOT; http://pastebin.com/wL6Z9g7r – Mehmet Emre Portakal Mar 20 '12 at 09:29
  • you are welcome. This service http://pastebin.com/swhd8eKi will serve both xml and json response depending on the request type. So you can see that i have a uniform method to serve both. Simply update your existing web-services with **ScriptService** and you are good to go. Good luck :) – waqaslam Mar 20 '12 at 09:34
  • @Waqas, I'm trying to supply what you explained here in iOS, using a similar of this code: http://stackoverflow.com/questions/9968042/passing-parameters-to-a-json-web-service-in-objective-c // however, I'm unable to make it work so far. do you have any clue how to make it work in iOS (above 5.0) – kubilay Apr 10 '12 at 07:27
  • sorry, I'm not an iOS developer, but i can help you with .NET part where your webservice is involved. See my answer :) – waqaslam Apr 10 '12 at 07:41
  • or perhaps post your webservice code online so i can see any anomalies... – waqaslam Apr 10 '12 at 08:10
  • @Waqas thanks, I've solved and posted the solution to related question. now I have a problem with returning data. NSJSONSerialization wants pure string with json objects but returning value starts with "" and ends with "". i already remove these strings and clean the response but it's not a solution really I think. what do you think I should do in order to remove xml headers in my web service? – kubilay Apr 10 '12 at 08:59
  • it only shows the xml stuff if you request through asmx webpage (by clicking the method name). But if you make request with json as header params, then it shouldnt include the xml stuff. Did you log the response of webmethod in phone? what exactly it returns? – waqaslam Apr 10 '12 at 09:06
  • @Waqas, please see the code and response: http://pastebin.com/9TraWRTE – kubilay Apr 10 '12 at 09:11
  • I think your request is not valid for Json. It should be `[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];` – waqaslam Apr 10 '12 at 09:30
  • @Waqas, that caused an error like this: http://jsfiddle.net/confeng/wRP3H/ – kubilay Apr 10 '12 at 09:36
  • actually now its working fine. but the error is due to invalid argument. are you passing the correct parameter name as required by your web-service? paste your webservice code too. Moreover, use http://jsonlint.com/ to format and read json strings for your own readability – waqaslam Apr 10 '12 at 09:44
  • 1
    if `sorgu=0` is your input parameter for web method then as a valid json parameter it should be `{"sorgu": 0}` – waqaslam Apr 10 '12 at 09:48
  • @Waqas thanks, that was the problem. – kubilay Apr 10 '12 at 11:18
  • you're welcome. happy jsonizing :) – waqaslam Apr 10 '12 at 11:19

1 Answers1

1

This may be related to the fact that in apis 11 and above accesing the net in Main thread is not allowed you may have to use ASYNC task.. I had an app which used JSON objects to get data , i had initially coded it simply in the onCreate (very small code) .. it worked fine on my phone.. but when i tried it on my tablet (ICS) it gave me errors, in the debug i saw an exception , when i googled it I found out the problem , hope it helps you, cheers!

Shark
  • 165
  • 1
  • 10