如何在Android SOAP Webservices中将InputStream数据转换为String

前端之家收集整理的这篇文章主要介绍了如何在Android SOAP Webservices中将InputStream数据转换为String前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我从 Android使用soap Web服务时,我希望在输出字符串中显示结果,如何将该输入流转换为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();
                }
        }


    }

我的最终代码请看一下,让我知道

我在哪里可以将转换放在上面的代码中?

解决方法

String  response = convertStreamToString(instream);

方法

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();
}
原文链接:https://www.f2er.com/android/317466.html

猜你在找的Android相关文章