Becky Reyna
Becky Reyna

Reputation: 97

Retrieve WebService Array Java(Android)

I have to check if user input the correct code in order to retrieve these information. Please help! Code below is how my SOAP looks like:

<retrieveAudioInfoResponse xmlns="http://tempuri.org/">
          <retrieveInfoResult>
            <Info>
              <date>string</date>
              <name>string</name>
              <id>int</id>
              <description>string</description>
              <url>string</url>
            </Info>

This is what I have done for my class to get data from web-service:

private void retrieveInfo(String code){

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("code", code);


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;     
        envelope.setOutputSoapObject(request);
        System.out.println(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {

               androidHttpTransport.call(SOAP_ACTION, envelope);
               SoapObject response = (SoapObject) envelope.getResponse();
               infoList = new String[response.getPropertyCount()];

               for(int i=0;i<response.getPropertyCount();i++){ 

               infoList[i] = response.getProperty(i).toString();
               Log.d("This is the response",infoList[i]);
      }      
} 
        catch (Exception e) {           
            e.printStackTrace();
        }       
    }

My Webservice:

POST /mobile/.../....asmx HTTP/1.1
Host: ...
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/retrieveInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <retrieveInfo xmlns="http://tempuri.org/">
      <code>string</code>
    </retrieveInfo>
  </soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <retrieveInfoResponse xmlns="http://tempuri.org/">
      <retrieveInfoResult>
        <Info>
          <date>string</date>
          <name>string</name>
          <id>int</id>
          <description>string</description>
          <url>string</url>
        </Info>
        <Info>
          <date>string</date>
          <name>string</name>
          <id>int</id>
          <description>string</description>
          <url>string</url>
        </Info>
      </retrieveInfoResult>
    </retrieveInfoResponse>
  </soap:Body>
</soap:Envelope>

The error that I received is:

01-31 13:15:00.708: D/dalvikvm(13583): GC_FOR_ALLOC freed 85K, 3% free 13108K/13447K, paused 12ms
01-31 13:15:00.713: I/dalvikvm-heap(13583): Grow heap (frag case) to 15.312MB for 2457616-byte allocation
01-31 13:15:00.738: D/dalvikvm(13583): GC_CONCURRENT freed 1K, 3% free 15507K/15879K, paused 2ms+2ms
01-31 13:15:00.788: D/dalvikvm(13583): GC_FOR_ALLOC freed 0K, 3% free 15507K/15879K, paused 13ms
01-31 13:15:00.798: I/dalvikvm-heap(13583): Grow heap (frag case) to 19.476MB for 4367376-byte allocation
01-31 13:15:00.823: D/dalvikvm(13583): GC_CONCURRENT freed 0K, 2% free 19772K/20167K, paused 2ms+2ms
01-31 13:15:00.898: W/System.err(13583): android.os.NetworkOnMainThreadException
01-31 13:15:00.898: W/System.err(13583):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-31 13:15:00.898: W/System.err(13583):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-31 13:15:00.898: W/System.err(13583):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-31 13:15:00.903: W/System.err(13583):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-31 13:15:00.903: W/System.err(13583):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-31 13:15:00.903: W/System.err(13583):    at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:75)
01-31 13:15:00.903: W/System.err(13583):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:136)
01-31 13:15:00.903: W/System.err(13583):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:90)
01-31 13:15:00.903: W/System.err(13583):    at com.example.loginws.second.WebServiceCallExample(second.java:80)
01-31 13:15:00.903: W/System.err(13583):    at com.example.loginws.second.onCreate(second.java:32)
01-31 13:15:00.908: W/System.err(13583):    at android.app.Activity.performCreate(Activity.java:4470)
01-31 13:15:00.908: W/System.err(13583):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-31 13:15:00.908: W/System.err(13583):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-31 13:15:00.908: W/System.err(13583):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-31 13:15:00.908: W/System.err(13583):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-31 13:15:00.908: W/System.err(13583):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-31 13:15:00.908: W/System.err(13583):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 13:15:00.908: W/System.err(13583):    at android.os.Looper.loop(Looper.java:137)
01-31 13:15:00.908: W/System.err(13583):    at android.app.ActivityThread.main(ActivityThread.java:4511)
01-31 13:15:00.908: W/System.err(13583):    at java.lang.reflect.Method.invokeNative(Native Method)
01-31 13:15:00.908: W/System.err(13583):    at java.lang.reflect.Method.invoke(Method.java:511)
01-31 13:15:00.908: W/System.err(13583):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-31 13:15:00.908: W/System.err(13583):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-31 13:15:00.913: W/System.err(13583):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 372

Answers (2)

Paresh Mayani
Paresh Mayani

Reputation: 128428

FYI, android.os.NetworkOnMainThreadException occurs whenever you implement long running task on Main UI Thread. So i am sure you are trying to call retrieveInfo() inside the onCreate() method directly.

The best solution is to make a web call inside doInBackground() method of AsyncTask. One more thing, AsyncTask is known as Painless Threading in android, because we programmer don't need to worry about management of threads once implemented.

Upvotes: 1

Sean F
Sean F

Reputation: 2390

What you want to do is make an XML schema to compare to.
You can run validation of a SOAP message against an XML schema.

Upvotes: 0

Related Questions