How to convert InputStream data to String in Android SOAP Webservices

When I consume soapy web services from Android, I want to display the result in the output line, how can I convert this 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 last code please look once and let me know

where can i put this conversion in the above code?

+6
source share
5 answers
  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(); } 
+14
source

Reading lines (both \ n and no difference) can cause confusion. To get a string from an InputStream, I suggest you copy / paste the following method and call it where 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(); } 
+3
source

Based on the Shane McC article, you can use 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(); } 
+2
source

You can try as follows:

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

str will contain the content, you need to analyze it further depending on the requirement. Also, see this link, there is also a link on how to parse it.

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

Try using VER11 soap instead of VER12, as this gives an error.

 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

// put all the necessary data into the soap

Additional information can be obtained at this link: http://groups.google.com/group/android-developers/browse_thread/thread/b585862b6e939fd2

0
source

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.

0
source

Source: https://habr.com/ru/post/916648/


All Articles