如何在 Android SOAP Webservices 中将 InputStream 数据转换为字符串
Posted
技术标签:
【中文标题】如何在 Android SOAP Webservices 中将 InputStream 数据转换为字符串【英文标题】:How can I convert InputStream data to String in Android SOAP Webservices 【发布时间】:2012-05-31 22:44:55 【问题描述】:当我从 android 使用肥皂网络服务时,我想在输出字符串中显示结果,如何将该输入流转换为 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();
我的最终代码请看一下并告诉我
我可以在上面的代码中将转换放在哪里?
【问题讨论】:
【参考方案1】: 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();
【讨论】:
简而言之,当你有 inputstrem 时,你可以附加我的意思 ust 现在我编辑了我的完整代码,请查看一次,让我知道为什么我的 logcat 中会出现异常 05-25 16:15:31.205:WARN/System.err(1458):org.xmlpull.v1.XmlPullParserException:预期:START_TAG w3.org/2001/12/soap-envelope信封(位置:START_TAG @1:686 in java.io.InputStreamReader @405457f8) 我在 SoapuI pro 中测试过,响应良好。通过 Android 我会遇到问题吗? 效果很好,除非你有一个大的输入流,然后它会抛出内存不足的错误。【参考方案2】:阅读行(\n 和 \r;没有区别)可能会造成混乱。要从 InputStream 中获取字符串,我建议您复制/粘贴以下方法并在需要时调用它。
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】:基于Shane McC article你可以使用这个方法:
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();
【讨论】:
【参考方案4】:import com.google.android.gms.common.util.IOUtils;
InputStream input = getInputStream();
String body = new String(IOUtils.toByteArray(input), "UTF-8");
input.close();
【讨论】:
【参考方案5】:你可以试试这个方法:
SoapObject response = (SoapObject)envelope.getResponse();
String str = response.getProperty(0).toString();
str 将保存内容,您需要根据需要进一步解析它。 另外,请查看此链接以及如何解析它的链接。
http://android-devblog.blogspot.com/2010/06/soap-on-android.html
尝试使用soap VER11而不是VER12,因为这会出错。
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//将所有需要的数据放入soap中
可以从此链接获得更多信息: http://groups.google.com/group/android-developers/browse_thread/thread/b585862b6e939fd2
【讨论】:
感谢您的回复我收到异常 05-25 16:15:31.205: WARN/System.err(1458): org.xmlpull.v1.XmlPullParserException: 预期: START_TAG w3.org/2001/12/soap-envelope 信封(位置:START_TAG schemas.xmlsoap.org/wsdlwsdl:definitions targetNamespace='urn:sap-com:document:sap:soap:functions:mc-style'>@1:686 in java.io.InputStreamReader@405457f8) @user1414667,对不起,我没有找到你。 Unni V Mana,刚才我编辑了我的完整代码,请看一次,让我知道为什么我的 logcat 中出现了这个异常【参考方案6】:你可以使用:
String response = org.apache.commons.io.IOUtils.toString(instream, "UTF-8");
您需要将 org.apache.commons.io.jar 添加到您的构建路径。
【讨论】:
该库在 android 上不可用 需要添加org.apache.commons.io.jar以上是关于如何在 Android SOAP Webservices 中将 InputStream 数据转换为字符串的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android SOAP Webservices 中将 InputStream 数据转换为字符串
Android,通过 HTTP POST (SOAP) 发送 XML
SOAP Web 服务不仅适用于 IOS 设备,它还适用于 soap ui 和 Android 设备
Android 的 SOAP 1.1、SOAP 1.2、HTTP GET 和 HTTP POST 方法有啥区别?