9

When I am consuming the soap web services from Android I want to display the result in output string how can I convert that Input Stream to Sting?

  package com.venkattt.pack;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.os.Bundle;

public class SoapWebservicesExampleActivity extends Activity {
    /** Called when the activity is first created. */
      final String NAMESPACE = "urn:sap-com:document:sap:soap:functions:mc-style";
     final String URL = "http://**********:8000/sap/bc/srt/wsdl/srvc_14DAE9C8D79F1EE196F1FC6C6518A345/wsdl11/allinone/ws_policy/document?sap-client=800&sap-user=************&sap-password=*********";
      final String METHOD_NAME = "Z_GET_CUST_GEN";
     final String SOAP_ACTION = "urn:sap-com:document:sap:soap:functions:mc-style/Z_GET_CUST_GEN";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
           SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); // set up
            request.addProperty("Input", "1460");
            request.addProperty("Langu", "d");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); // put all required data into a soap
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(URL);
            httpTransport.debug = true;

            try {


                 httpTransport.call(SOAP_ACTION, envelope);
                 SoapObject response = (SoapObject)envelope.getResponse();
                 String str = response.getProperty(0).toString();

                 System.out.println("theeeeeeeeeee"+str);



                }
            catch(SocketException ex){
                    ex.printStackTrace();

                } catch (Exception e) {
                   e.printStackTrace();
                }
        }


    }

my final code please look at once and let me know

where can I put that conversion in the above code?

Regexident
  • 29,441
  • 10
  • 93
  • 100
user1414667
  • 91
  • 1
  • 1
  • 4

6 Answers6

22
   String  response = convertStreamToString(instream);

Method

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
Jon
  • 9,156
  • 9
  • 56
  • 73
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • in short where you want you can append i mean when you have inputstrem – Samir Mangroliya May 25 '12 at 10:44
  • ust now i edited my full code please see once and let me know why that exception come in my logcat – user1414667 May 25 '12 at 11:28
  • 05-25 16:15:31.205: WARN/System.err(1458): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {w3.org/2001/12/soap-envelope}Envelope (position:START_TAG <{schemas.xmlsoap.org/wsdl}wsdl:definitions targetNamespace='urn:sap-com:document:sap:soap:functions:mc-style'>@1:686 in java.io.InputStreamReader@405457f8) – user1414667 May 25 '12 at 11:28
  • i tested in SoapuI pro the response comming is good.through Android I ma facing the Problem ? – user1414667 May 25 '12 at 11:31
  • Works well unless you have a large inputstream, then it throws out of memory errors. – Behr Sep 26 '14 at 14:55
3

Reading lines (both \n and \r; no distinctions) can make a mess. To get a String from an InputStream, I suggest you copy/paste the following method and call it wherever you need it.

public static String getStringFromInputStream(InputStream stream, String charsetName) throws IOException
{
    int n = 0;
    char[] buffer = new char[1024 * 4];
    InputStreamReader reader = new InputStreamReader(stream, charsetName);
    StringWriter writer = new StringWriter();
    while (-1 != (n = reader.read(buffer))) writer.write(buffer, 0, n);
    return writer.toString();
}
Budimir Grom
  • 756
  • 6
  • 12
2

Based on Shane McC article you can using this method:

public String readFully(InputStream entityResponse) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = entityResponse.read(buffer)) != -1) {
    baos.write(buffer, 0, length);
}
return baos.toString();
}
bebosh
  • 806
  • 10
  • 25
2
import com.google.android.gms.common.util.IOUtils;

InputStream input = getInputStream();
String body = new String(IOUtils.toByteArray(input), "UTF-8");
input.close();
spinyBabbler
  • 392
  • 5
  • 19
Valdemar_Rudolfovich
  • 3,021
  • 2
  • 18
  • 17
0

You can try this way:

SoapObject response = (SoapObject)envelope.getResponse();
String str = response.getProperty(0).toString();

str will hold the content, you need to parse it further depending upon the requirement. Also, please look this link as well there is a link how to parse it.

http://android-devblog.blogspot.com/2010/06/soap-on-android.html

Try using soap VER11 instead of VER12 as this is giving error.

SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11); 

// put all required data into a soap

More information can be had about this from this link: http://groups.google.com/group/android-developers/browse_thread/thread/b585862b6e939fd2

UVM
  • 9,776
  • 6
  • 41
  • 66
  • thanks for ur reply I am getting the exception 05-25 16:15:31.205: WARN/System.err(1458): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://www.w3.org/2001/12/soap-envelope}Envelope (position:START_TAG <{http://schemas.xmlsoap.org/wsdl/}wsdl:definitions targetNamespace='urn:sap-com:document:sap:soap:functions:mc-style'>@1:686 in java.io.InputStreamReader@405457f8) – user1414667 May 25 '12 at 10:46
  • @user1414667, sorry I did not get you. – UVM May 25 '12 at 11:03
  • Unni V Mana ,just now i edited my full code please see once and let me know why that exception come in my logcat – user1414667 May 25 '12 at 11:22
0

You can use:

String response = org.apache.commons.io.IOUtils.toString(instream, "UTF-8");

You need to add org.apache.commons.io.jar to your build path.

Arun George
  • 1,167
  • 3
  • 15
  • 29